diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-03 03:49:53 +0000 |
commit | f4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch) | |
tree | 13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/old/multilogtext.cpp | |
parent | 74d4c8cd27334fc7204d5a8773deb3d424565778 (diff) | |
download | libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2 libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip |
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a
unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/old/multilogtext.cpp')
-rw-r--r-- | src/old/multilogtext.cpp | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/src/old/multilogtext.cpp b/src/old/multilogtext.cpp new file mode 100644 index 0000000..4337cc9 --- /dev/null +++ b/src/old/multilogtext.cpp | |||
@@ -0,0 +1,188 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <fcntl.h> | ||
4 | #include <unistd.h> | ||
5 | #include <time.h> | ||
6 | #include <string.h> | ||
7 | #include "multilogtext.h" | ||
8 | /* | ||
9 | bool fileexists( const char *sPath ) | ||
10 | { | ||
11 | int nFileDesc = open( sPath, O_RDONLY ); | ||
12 | if( nFileDesc < 0 ) | ||
13 | { | ||
14 | return false; | ||
15 | } | ||
16 | else | ||
17 | { | ||
18 | close( nFileDesc ); | ||
19 | return true; | ||
20 | } | ||
21 | }*/ | ||
22 | |||
23 | MultiLogText::MultiLogText( const char *sFileName, const char *lpFormat, bool bRotateLog, int nMaxLogs ) | ||
24 | { | ||
25 | this->lpFormat = NULL; | ||
26 | /* | ||
27 | if( bRotateLog ) | ||
28 | { | ||
29 | if( fileexists( sFileName ) == false ) | ||
30 | { | ||
31 | return; | ||
32 | } | ||
33 | |||
34 | int nLen = strlen( sFileName ); | ||
35 | char *buf = new char[nLen+6]; | ||
36 | sprintf( buf, "%s.", sFileName ); | ||
37 | |||
38 | for( int j = 1; j < nMaxLogs; j++ ) | ||
39 | { | ||
40 | sprintf( &buf[nLen+1], "%d", j ); | ||
41 | if( !fileexists( buf ) ) | ||
42 | { | ||
43 | rename( sFileName, buf ); | ||
44 | break; | ||
45 | } | ||
46 | } | ||
47 | }*/ | ||
48 | |||
49 | nFD = open( sFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ); | ||
50 | setLogFormat( lpFormat ); | ||
51 | } | ||
52 | |||
53 | MultiLogText::MultiLogText( int nFileDesc, const char *lpFormat ) | ||
54 | { | ||
55 | this->lpFormat = NULL; | ||
56 | nFD = nFileDesc; | ||
57 | setLogFormat( lpFormat ); | ||
58 | } | ||
59 | |||
60 | MultiLogText::~MultiLogText() | ||
61 | { | ||
62 | if( nFD != -1 ) | ||
63 | { | ||
64 | close( nFD ); | ||
65 | } | ||
66 | |||
67 | delete[] lpFormat; | ||
68 | } | ||
69 | |||
70 | bool MultiLogText::setLogFormat( const char *lpFormat ) | ||
71 | { | ||
72 | char buf[200]; | ||
73 | int k = 0; | ||
74 | static char fmts[10][4]={ | ||
75 | {'y', 'd', '0', '1'}, | ||
76 | {'m', 'd', '0', '2'}, | ||
77 | {'d', 'd', '0', '3'}, | ||
78 | {'h', 'd', '0', '4'}, | ||
79 | {'M', 'd', '0', '5'}, | ||
80 | {'s', 'd', '0', '6'}, | ||
81 | {'l', 'd', '0', '7'}, | ||
82 | {'f', 's', '0', '8'}, | ||
83 | {'L', 'd', '0', '9'}, | ||
84 | {'t', 's', '1', '0'}, | ||
85 | }; | ||
86 | |||
87 | for( int j = 0; lpFormat[j] != '\0'; j++ ) | ||
88 | { | ||
89 | if( lpFormat[j] == '%' ) | ||
90 | { | ||
91 | buf[k++] = '%'; | ||
92 | int nPlace = k++; | ||
93 | k++; | ||
94 | buf[k++] = '$'; | ||
95 | bool bDone = false; | ||
96 | for( j++; bDone == false; j++ ) | ||
97 | { | ||
98 | int l; | ||
99 | for( l = 0; l < 10; l++ ) | ||
100 | { | ||
101 | if( lpFormat[j] == fmts[l][0] ) | ||
102 | { | ||
103 | buf[nPlace] = fmts[l][2]; | ||
104 | buf[nPlace+1] = fmts[l][3]; | ||
105 | buf[k++] = fmts[l][1]; | ||
106 | bDone = true; | ||
107 | break; | ||
108 | } | ||
109 | } | ||
110 | if( l == 10 ) | ||
111 | { | ||
112 | buf[k++] = lpFormat[j]; | ||
113 | } | ||
114 | } | ||
115 | j--; | ||
116 | } | ||
117 | else | ||
118 | { | ||
119 | buf[k++] = lpFormat[j]; | ||
120 | } | ||
121 | } | ||
122 | buf[k++] = '\n'; | ||
123 | buf[k] = '\0'; | ||
124 | |||
125 | if( this->lpFormat != NULL ) | ||
126 | { | ||
127 | delete[] this->lpFormat; | ||
128 | } | ||
129 | this->lpFormat = new char[k+1]; | ||
130 | strcpy( this->lpFormat, buf ); | ||
131 | |||
132 | return true; | ||
133 | } | ||
134 | |||
135 | bool MultiLogText::openLog() | ||
136 | { | ||
137 | if( nFD == -1 ) | ||
138 | { | ||
139 | return false; | ||
140 | } | ||
141 | return true; | ||
142 | } | ||
143 | |||
144 | bool MultiLogText::append( MultiLog::LogEntry *pEntry ) | ||
145 | { | ||
146 | if( nFD == -1 ) | ||
147 | { | ||
148 | return false; | ||
149 | } | ||
150 | |||
151 | char *line = NULL; | ||
152 | struct tm *pTime; | ||
153 | pTime = localtime( &pEntry->xTime ); | ||
154 | asprintf( | ||
155 | &line, | ||
156 | lpFormat, | ||
157 | pTime->tm_year+1900, | ||
158 | pTime->tm_mon+1, | ||
159 | pTime->tm_mday, | ||
160 | pTime->tm_hour, | ||
161 | pTime->tm_min, | ||
162 | pTime->tm_sec, | ||
163 | pEntry->nLevel, | ||
164 | pEntry->lpFile, | ||
165 | pEntry->nLine, | ||
166 | pEntry->lpText | ||
167 | ); | ||
168 | write( nFD, line, strlen(line) ); | ||
169 | free( line ); | ||
170 | |||
171 | return true; | ||
172 | } | ||
173 | |||
174 | bool MultiLogText::closeLog() | ||
175 | { | ||
176 | if( nFD == -1 ) | ||
177 | { | ||
178 | return false; | ||
179 | } | ||
180 | // Don't close it if it's sdtout or errorout | ||
181 | if( nFD > 2 ) | ||
182 | { | ||
183 | close( nFD ); | ||
184 | } | ||
185 | nFD = -1; | ||
186 | return true; | ||
187 | } | ||
188 | |||