-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.cpp
151 lines (129 loc) · 3.42 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
** Author: Rachel Flores-Meath
** Last Updated: March 20, 2012
** General log file utilities.
** Contains methods to open and close a log file, to append a pre-formatted
** string to a log file, to append a string and result to a log file,
** and to write to a previously closed log file
** append_action_to_closed_log() is most useful for quick writes
** The other methods are geared towards multiple writes in succession
** (Really, they're there for modularity and efficiency)
** open_log_file() is the only method that explicitly defines where the
** log file is (i.e. log.txt in the current working directory)
** The remaining methods rely on a file handler for modularity
**
** WARNING: These methods are NOT THREAD-SAFE!
** The application should not need to write to the log in a threaded state
** If we decide it does, this library will be rewritten to reflect that
**/
#include <iostream>
#include <fstream>
using namespace std;
/*
* Given a file stream, opens that filestream to log.txt
* Throws log_fail_ex on failure
*/
void open_log_file(fstream &log)
{
if (log.is_open())
{
throw log_fail_ex;
}
log.open("log.txt", ios::out | ios::app);
if (!log.is_open())
{
throw log_fail_ex;
}
}
/*
* Given a file stream, cleanly closes the file
*/
void close_log_file(fstream& log)
{
if (log.is_open())
{
log.close();
}
//If the log is already closed, we have nothing to worry about
}
/*
* Given a (pre-formatted) string and file stream, appends the string to the log file
* Places a timestamp at the start of the string
* Places a newline at the end of the string if one is not at the end of the string
* Throws log_fail_ex if the log is not open
*/
void append_raw_to_log(fstream& log, string s)
{
if (!log.is_open())
{
throw log_fail_ex;
}
time_t current_time;
time(¤t_time);
log << ctime(¤t_time) << s;
if (s[s.size() - 1] != '\n')
{
log << endl;
}else
{
flush(log);
}
if (log.fail())
{
throw log_fail_ex;
}
}
/*
* Given a string containing an action that occurred and the result of that action, appends the string and the result to the log file
* Places a timestamp at the start of the string
* Places a newline at the end of the string
* Throws log_fail_ex if the log is not open
*/
void append_action_to_log(fstream& log, string s, bool result)
{
if (!log.is_open())
{
throw log_fail_ex;
}
time_t current_time;
time(¤t_time);
//FIXME remove newline char at the end of ctime
log << ctime(¤t_time) << ": " << s;
if (result)
{
log << " (SUCCESS)";
}else
{
log << " (FAILURE)";
}
log << endl;
if (log.fail())
{
throw log_fail_ex;
}
}
/*
* Given a string containing an action that occurred and the result of that action, appends the string and the result to the log file
* Performs the following methods in order:
* open_log_file(log)
* append_action_to_log(log, s, result)
* close_log_file(log)
* Throws log_fail_ex if any failures occurred
*
* This will be hugely inefficient if multiple writes to the log file
* are needed in succession
*/
void append_action_to_closed_log(string s, bool result)
{
try
{
fstream log;
open_log_file(log);
append_action_to_log(log, s, result);
close_log_file(log);
}catch(log_fail_exception e)
{
//Not necessary, but could be useful for later modifications
throw log_fail_ex;
}
}