diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-03 00:28:59 +0000 |
| commit | ac517a2b7625e0aa0862679e961c6349f859ea3b (patch) | |
| tree | e3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/logger.cpp | |
| parent | f8d4301e9fa4f3709258505941e37fab2eadadc6 (diff) | |
| parent | bd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff) | |
| download | libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2 libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip | |
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to '')
| -rw-r--r-- | src/logger.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..1fc2262 --- /dev/null +++ b/src/logger.cpp | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | #include "bu/logger.h" | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <time.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | Bu::Logger::Logger() | ||
| 7 | { | ||
| 8 | } | ||
| 9 | |||
| 10 | Bu::Logger::~Logger() | ||
| 11 | { | ||
| 12 | } | ||
| 13 | |||
| 14 | void 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 | |||
| 50 | void 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 | Bu::FString sBuf; | ||
| 75 | for(;;) | ||
| 76 | { | ||
| 77 | s++; | ||
| 78 | int l; | ||
| 79 | for( l = 0;; l++ ) | ||
| 80 | { | ||
| 81 | if( fmts[l][0] == '\0' ) | ||
| 82 | { | ||
| 83 | sBuf += *s; | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | else if( *s == fmts[l][0] ) | ||
| 87 | { | ||
| 88 | sLogFormat += fmts[l][2]; | ||
| 89 | sLogFormat += fmts[l][3]; | ||
| 90 | sLogFormat += '$'; | ||
| 91 | sLogFormat += sBuf; | ||
| 92 | sLogFormat += fmts[l][1]; | ||
| 93 | break; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | if( fmts[l][0] != '\0' ) | ||
| 97 | break; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | sLogFormat += *s; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | sLogFormat += '\n'; | ||
| 106 | |||
| 107 | write( fileno(stdout), sLogFormat.getStr(), sLogFormat.getSize() ); | ||
| 108 | } | ||
| 109 | |||
| 110 | void Bu::Logger::setMask( int n ) | ||
| 111 | { | ||
| 112 | nLevelMask = n; | ||
| 113 | } | ||
| 114 | |||
| 115 | void Bu::Logger::setLevel( int n ) | ||
| 116 | { | ||
| 117 | int j; | ||
| 118 | for( j = 31; j > 0; j-- ) | ||
| 119 | { | ||
| 120 | if( (n&(1<<j)) ) | ||
| 121 | { | ||
| 122 | for(; j >= 0; j-- ) | ||
| 123 | { | ||
| 124 | n |= (1<<j); | ||
| 125 | } | ||
| 126 | nLevelMask = n; | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
