diff options
Diffstat (limited to 'src/multilogtext.cpp')
-rw-r--r-- | src/multilogtext.cpp | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/multilogtext.cpp b/src/multilogtext.cpp index be64595..daad4c0 100644 --- a/src/multilogtext.cpp +++ b/src/multilogtext.cpp | |||
@@ -1,4 +1,3 @@ | |||
1 | |||
2 | #include <stdio.h> | 1 | #include <stdio.h> |
3 | #include <stdlib.h> | 2 | #include <stdlib.h> |
4 | #include <fcntl.h> | 3 | #include <fcntl.h> |
@@ -7,9 +6,46 @@ | |||
7 | #include <string.h> | 6 | #include <string.h> |
8 | #include "multilogtext.h" | 7 | #include "multilogtext.h" |
9 | 8 | ||
10 | MultiLogText::MultiLogText( const char *sFileName, const char *lpFormat ) | 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 ) | ||
11 | { | 24 | { |
12 | this->lpFormat = NULL; | 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 | |||
13 | nFD = open( sFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ); | 49 | nFD = open( sFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH ); |
14 | setLogFormat( lpFormat ); | 50 | setLogFormat( lpFormat ); |
15 | } | 51 | } |