"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "logger.cc" see the
Fossies "Dox" file reference documentation.
1 /*
2 * xstress - xk0derz SMTP Stress Tester
3 *
4 * (c) Amit Singh amit@xkoder.com
5 * http://xkoder.com
6 *
7 * This software and related files are licensed under GNU GPL version 2
8 * Please visit the following webpage for more details
9 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 */
11 #include <fstream>
12 #include "common.h"
13 #include "logger.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 using namespace std;
18
19
20 Logger::Logger(string _LogFile,int _cacheThreshold)
21 {
22 threadId = -1;
23 sCache = "";
24 sLogFile = _LogFile;
25 iThreshold = _cacheThreshold;
26 }
27
28 void Logger::setThreadId(int _id)
29 {
30 threadId = _id;
31 }
32
33 int Logger::getThreadId()
34 {
35 return threadId;
36 }
37
38 void Logger::resetThreshold(int _cacheThreshold)
39 {
40 iThreshold = _cacheThreshold;
41 }
42
43 void Logger::log(string _msg)
44 {
45 char cBuffer[255];
46 struct tm *currTime;
47 time_t currTS;
48
49 time(&currTS);
50 currTime = localtime(&currTS);
51
52 sprintf(cBuffer, "%i/%i/%i %02i:%02i:%02i - Thread(%i) ",
53 1900+currTime->tm_year,
54 currTime->tm_mon+1,
55 currTime->tm_mday,
56 currTime->tm_hour, currTime->tm_min, currTime->tm_sec,
57 threadId);
58
59 sCache.append(cBuffer);
60 sCache.append(_msg);
61 sCache.append("\n");
62 if(sCache.size()>iThreshold)
63 {
64 flush();
65 }
66 }
67
68 void Logger::flush()
69 {
70 int fd=-1;
71
72 do
73 {
74 fd = open(".xstress.log.lock",O_WRONLY | O_CREAT | O_EXCL);
75 if(fd<0 && errno==EEXIST) continue;
76 else if(fd<0)
77 {
78 remove(".xstress.log.lock");
79 cout << "ERROR: Cannot take log lock" << endl;
80 return;
81 }
82 }while(0);
83
84
85 fstream filp(sLogFile.c_str(), ios::app | ios::out);
86
87 if(!filp.fail())
88 {
89 filp.write(sCache.c_str(), sCache.size());
90 sCache = "";
91 filp.close();
92 }
93 else
94 {
95 cout << "ERROR: Unable to write to log file `" << sLogFile << "'" << endl;
96 }
97
98 if(fd>=0) close(fd);
99 remove(".xstress.log.lock");
100 }
101
102 Logger::~Logger()
103 {
104 flush();
105 }
106
107 void Logger::setLogFile(string sFileName)
108 {
109 sLogFile = sFileName;
110 }