aboutsummaryrefslogtreecommitdiff
path: root/src/multilogtext.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/multilogtext.cpp152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/multilogtext.cpp b/src/multilogtext.cpp
new file mode 100644
index 0000000..be64595
--- /dev/null
+++ b/src/multilogtext.cpp
@@ -0,0 +1,152 @@
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <time.h>
7#include <string.h>
8#include "multilogtext.h"
9
10MultiLogText::MultiLogText( const char *sFileName, const char *lpFormat )
11{
12 this->lpFormat = NULL;
13 nFD = open( sFileName, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH );
14 setLogFormat( lpFormat );
15}
16
17MultiLogText::MultiLogText( int nFileDesc, const char *lpFormat )
18{
19 this->lpFormat = NULL;
20 nFD = nFileDesc;
21 setLogFormat( lpFormat );
22}
23
24MultiLogText::~MultiLogText()
25{
26 if( nFD != -1 )
27 {
28 close( nFD );
29 }
30
31 delete[] lpFormat;
32}
33
34bool MultiLogText::setLogFormat( const char *lpFormat )
35{
36 char buf[200];
37 int k = 0;
38 static char fmts[10][4]={
39 {'y', 'd', '0', '1'},
40 {'m', 'd', '0', '2'},
41 {'d', 'd', '0', '3'},
42 {'h', 'd', '0', '4'},
43 {'M', 'd', '0', '5'},
44 {'s', 'd', '0', '6'},
45 {'l', 'd', '0', '7'},
46 {'f', 's', '0', '8'},
47 {'L', 'd', '0', '9'},
48 {'t', 's', '1', '0'},
49 };
50
51 for( int j = 0; lpFormat[j] != '\0'; j++ )
52 {
53 if( lpFormat[j] == '%' )
54 {
55 buf[k++] = '%';
56 int nPlace = k++;
57 k++;
58 buf[k++] = '$';
59 bool bDone = false;
60 for( j++; bDone == false; j++ )
61 {
62 int l;
63 for( l = 0; l < 10; l++ )
64 {
65 if( lpFormat[j] == fmts[l][0] )
66 {
67 buf[nPlace] = fmts[l][2];
68 buf[nPlace+1] = fmts[l][3];
69 buf[k++] = fmts[l][1];
70 bDone = true;
71 break;
72 }
73 }
74 if( l == 10 )
75 {
76 buf[k++] = lpFormat[j];
77 }
78 }
79 j--;
80 }
81 else
82 {
83 buf[k++] = lpFormat[j];
84 }
85 }
86 buf[k++] = '\n';
87 buf[k] = '\0';
88
89 if( this->lpFormat != NULL )
90 {
91 delete[] this->lpFormat;
92 }
93 this->lpFormat = new char[k+1];
94 strcpy( this->lpFormat, buf );
95
96 return true;
97}
98
99bool MultiLogText::openLog()
100{
101 if( nFD == -1 )
102 {
103 return false;
104 }
105 return true;
106}
107
108bool MultiLogText::append( MultiLog::LogEntry *pEntry )
109{
110 if( nFD == -1 )
111 {
112 return false;
113 }
114
115 char *line = NULL;
116 struct tm *pTime;
117 pTime = localtime( &pEntry->xTime );
118 asprintf(
119 &line,
120 lpFormat,
121 pTime->tm_year+1900,
122 pTime->tm_mon+1,
123 pTime->tm_mday,
124 pTime->tm_hour,
125 pTime->tm_min,
126 pTime->tm_sec,
127 pEntry->nLevel,
128 pEntry->lpFile,
129 pEntry->nLine,
130 pEntry->lpText
131 );
132 write( nFD, line, strlen(line) );
133 free( line );
134
135 return true;
136}
137
138bool MultiLogText::closeLog()
139{
140 if( nFD == -1 )
141 {
142 return false;
143 }
144 // Don't close it if it's sdtout or errorout
145 if( nFD > 2 )
146 {
147 close( nFD );
148 }
149 nFD = -1;
150 return true;
151}
152