File: test_alog.cpp

package info (click to toggle)
libcommoncpp2 1.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,300 kB
  • sloc: cpp: 29,221; sh: 10,352; ansic: 1,134; makefile: 238; xml: 5
file content (120 lines) | stat: -rw-r--r-- 3,542 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * =====================================================================================
 *
 *       Filename:  test_alog.cpp
 *
 *    Description:  file to test alog and AppLog
 *
 *        Version:  1.0
 *        Created:  17/03/2009 14:49:21
 *       Compiler:  g++
 *
 *         Author:  Angelo Naselli (an), anaselli@linux.it (C) 2009
 *      Copyright:  See COPYING file that comes with this distribution
 *
 * =====================================================================================
 */

#include <cc++/thread.h>
#include <cc++/applog.h>

#ifdef  CCXX_NAMESPACES
using namespace ost;
#endif

class ThreadTest: public Thread
{
    static AppLog::Ident mod_name;
    void final ( void );
    void initial ( void );

    int _id;
  public:
    ThreadTest ( int id );
    void run();
};

AppLog::Ident  ThreadTest::mod_name ( "ThreadTest" );

ThreadTest::ThreadTest ( int id ) : Thread(), _id ( id )
{
}

void ThreadTest::final()
{
  alog << mod_name << info << __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog.unsubscribe();
}

void ThreadTest::initial()
{
  //Init applog data
  alog.subscribe();
  alog.level ( Slog::levelInfo );
  alog << mod_name << info << __PRETTY_FUNCTION__ << " id " << _id << endl;
}

void ThreadTest::run()
{
  alog << mod_name << debug    <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << info     <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << warn     <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << alert    <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  Thread::sleep ( 10 );
  alog << mod_name << error    <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << notice   <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << critical <<  __PRETTY_FUNCTION__ << " id " << _id << endl;
  alog << mod_name << emerg    <<  __PRETTY_FUNCTION__ << " id " << _id << endl;

  Thread::sleep ( 100 );
}


int main()
{
  AppLog::Ident mod_name ( "main" );
  // uncomment third parameter to test log over pipe
  alog.logFileName ( "./test_alog.log", false /*, true */);
  alog.subscribe();
  alog.level ( Slog::levelDebug );
  alog.slogEnable ( false ); // default false
  alog.clogEnable ( true );
  alog << mod_name << info   <<  "testAlog V. 1.0.0" << std::endl;
  alog.clogEnable ( false );

  // change level for module ThreadTest to info
  alog.identLevel ( "ThreadTest", AppLog::levelTranslate ( "warn" ) );

  alog << mod_name << debug << __PRETTY_FUNCTION__ << " Starting t1 warn level" << endl;
  ThreadTest t1 ( 1 );
  t1.start();
  t1.join();
  alog << mod_name << debug << __PRETTY_FUNCTION__ << " t1 finished" << endl;

  alog << mod_name << debug << __PRETTY_FUNCTION__ << " Starting t2 and t3 enabling debug level" << endl;
  alog.identLevel ( "ThreadTest", AppLog::levelTranslate ( "debug" ) );
  ThreadTest t2 ( 2 ), t3 ( 3 );
  t2.start();
  t3.start();
  alog << mod_name << info << __PRETTY_FUNCTION__ << " Waiting 1000 ms" << endl;

  Thread::sleep ( 1000 );

  alog << mod_name << warn << __PRETTY_FUNCTION__ << " end test" << endl;

  alog << mod_name << info << "open a new log file, called testAlog.log" << endl;
  AppLog newLog ( "./testAlog.log" );
  newLog.subscribe();
  newLog << mod_name << info << "This is a new log" << endl;

  // HEXdump testing
  unsigned char buff[10] ;
  for (int i=0; i<10; i++) buff[i] = i+48;

  newLog << info << HEXdump(buff, 10) << std::endl;

  newLog << info <<  __PRETTY_FUNCTION__ << " end test" << endl;

  return 0;
}