Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 79 lines (70 sloc) 1.52 kB
220b8a9 @tylerkaraszewski first commit
authored
1 #include "Logger.h"
2 #include "Utils.h"
3 #include <errno.h>
4 #include <time.h>
5 #include <string.h>
6 #include <stdio.h>
7
8 using std::string;
9
23dd6fc @tylerkaraszewski Adds config file support.
authored
10 Logger::Logger(const Config& config) :
11 m_config(config),
220b8a9 @tylerkaraszewski first commit
authored
12 m_accessFile(NULL),
13 m_errorFile(NULL)
14 {
15 }
16
17 bool Logger::open()
18 {
23dd6fc @tylerkaraszewski Adds config file support.
authored
19 string accessFilePath = m_config.getString(Config::S_KEY_ACCESS_FILE_PATH);
20 string errorFilePath = m_config.getString(Config::S_KEY_ERROR_FILE_PATH);
21
220b8a9 @tylerkaraszewski first commit
authored
22 close();
23dd6fc @tylerkaraszewski Adds config file support.
authored
23 FILE* access = fopen(accessFilePath.c_str(), "a");
220b8a9 @tylerkaraszewski first commit
authored
24 if (access == NULL)
25 {
26 return false;
27 }
23dd6fc @tylerkaraszewski Adds config file support.
authored
28 FILE* error = fopen(errorFilePath.c_str(), "a");
220b8a9 @tylerkaraszewski first commit
authored
29 if (error == NULL)
30 {
31 return false;
32 }
33 m_accessFile = access;
34 m_errorFile = error;
35 return true;
36 }
37
38 void Logger::close()
39 {
40 if (m_accessFile != NULL)
41 {
42 fclose(m_accessFile);
43 m_accessFile = NULL;
44 }
45 if (m_errorFile != NULL)
46 {
47 fclose(m_errorFile);
48 m_errorFile = NULL;
49 }
50 }
51
52 void Logger::logAccess(const string& msg)
53 {
54 log(m_accessFile, msg);
55 }
56
57 void Logger::logError(const string& msg)
58 {
59 log(m_errorFile, msg);
60 }
61
62 void Logger::log(FILE* file, const string& msg)
63 {
64 if (file != NULL)
65 {
66 string logline = Utils::llToString(time(NULL)) + "\t" + msg + "\n";
67 size_t bytesWritten = fwrite(logline.c_str(), 1, logline.size(), file);
68 if (bytesWritten != logline.size())
69 {
70 // This wont actually go anywhere.
71 printf("Log writing error %d (%s).\n", errno, strerror(errno));
72 }
73 else
74 {
75 fflush(file); // Can take this out for performance reasons if required.
76 }
77 }
78 }
Something went wrong with that request. Please try again.