aboutsummaryrefslogtreecommitdiff
path: root/src/logger.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-06-26 05:10:58 +0000
committerMike Buland <eichlan@xagasoft.com>2007-06-26 05:10:58 +0000
commitaa82dc64b397b6ca0d336d91638d4f4b849e3667 (patch)
treec17f285bf17f94812748424b854e8c3e437c25c2 /src/logger.cpp
parent3f26c19b0b7a9fa73c58189788972ea43b72f014 (diff)
downloadlibbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.gz
libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.bz2
libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.tar.xz
libbu++-aa82dc64b397b6ca0d336d91638d4f4b849e3667.zip
Fixed a minor bug in FString, and added the Logger and a test...it's cool, and
a decent replacement for multilog now that we use runit.
Diffstat (limited to 'src/logger.cpp')
-rw-r--r--src/logger.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/logger.cpp b/src/logger.cpp
new file mode 100644
index 0000000..848dfb1
--- /dev/null
+++ b/src/logger.cpp
@@ -0,0 +1,123 @@
1#include "bu/logger.h"
2#include <stdarg.h>
3#include <time.h>
4#include <stdio.h>
5
6Bu::Logger::Logger()
7{
8}
9
10Bu::Logger::~Logger()
11{
12}
13
14void Bu::Logger::log( int nLevel, const char *sFile, const char *sFunction, int nLine, const char *sFormat, ...)
15{
16 if( (nLevel&nLevelMask) == 0 )
17 return;
18
19 va_list ap;
20 va_start( ap, sFormat );
21 char *text;
22 vasprintf( &text, sFormat, ap );
23 va_end(ap);
24
25 time_t t = time(NULL);
26
27 char *line = NULL;
28 struct tm *pTime;
29 pTime = localtime( &t );
30 asprintf(
31 &line,
32 sLogFormat.getStr(),
33 pTime->tm_year+1900,
34 pTime->tm_mon+1,
35 pTime->tm_mday,
36 pTime->tm_hour,
37 pTime->tm_min,
38 pTime->tm_sec,
39 nLevel,
40 sFile,
41 nLine,
42 text,
43 sFunction
44 );
45 write( fileno(stdout), line, strlen(line) );
46 free( text );
47 free( line );
48}
49
50void Bu::Logger::setFormat( const Bu::FString &str )
51{
52 sLogFormat = "";
53
54 static char fmts[][4]={
55 {'y', 'd', '0', '1'},
56 {'m', 'd', '0', '2'},
57 {'d', 'd', '0', '3'},
58 {'h', 'd', '0', '4'},
59 {'M', 'd', '0', '5'},
60 {'s', 'd', '0', '6'},
61 {'L', 'd', '0', '7'},
62 {'f', 's', '0', '8'},
63 {'l', 'd', '0', '9'},
64 {'t', 's', '1', '0'},
65 {'F', 's', '1', '1'},
66 {'\0', '\0', '\0', '\0'},
67 };
68
69 for( const char *s = str.getStr(); *s; s++ )
70 {
71 if( *s == '%' )
72 {
73 sLogFormat += '%';
74 s++;
75 for( int l = 0;; l++ )
76 {
77 if( fmts[l][0] == '\0' )
78 {
79 sLogFormat += *s;
80 break;
81 }
82 else if( *s == fmts[l][0] )
83 {
84 sLogFormat += fmts[l][2];
85 sLogFormat += fmts[l][3];
86 sLogFormat += '$';
87 sLogFormat += fmts[l][1];
88 break;
89 }
90 }
91 }
92 else
93 {
94 sLogFormat += *s;
95 }
96 }
97 sLogFormat += '\n';
98
99 write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() );
100}
101
102void Bu::Logger::setMask( int n )
103{
104 nLevelMask = n;
105}
106
107void Bu::Logger::setLevel( int n )
108{
109 int j;
110 for( j = 31; j > 0; j-- )
111 {
112 if( (n&(1<<j)) )
113 {
114 for(; j >= 0; j-- )
115 {
116 n |= (1<<j);
117 }
118 nLevelMask = n;
119 return;
120 }
121 }
122}
123