-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
71 lines (54 loc) · 1.37 KB
/
log.cpp
File metadata and controls
71 lines (54 loc) · 1.37 KB
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
// Contains Log class
#include "log.hpp"
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
Log* Log::p_Inst = NULL;
Log::Log(){}
time_t Log::timer;
Log* Log::Inst(){
if( !p_Inst ){
p_Inst = new Log;
// initialize timer
time( &timer );
}
return p_Inst;
}
// initalize logfile
void Log::open_log( string logfile ){
log_fs.open( logfile, fstream::out | fstream::trunc );
// ensure logfile has opened properly
if( log_fs.fail() ){
fprintf( stderr, "Error: Could not open logfile '%s'. Check permissions for logfile location", logfile.c_str() );
exit(1);
}
}
// closes logfile
void Log::close_log(){
log_fs << get_time() << "\tProcess terminated" << endl;
log_fs.close();
}
// prints notes to file as the program progresses
void Log::log_it( string note ){
if( log_output ){
log_fs << get_time() << "\t" << note << endl;
}
if( screen_output ){
cout << get_time() << "\t" << note << endl;
}
}
// return a string of the current time in the program
string Log::get_time(){
string curr_time = "";
char buffer[100];
int t_now = difftime( time(0), timer ); // get time now
int t_hour = t_now / 3600;
t_now = t_now % 3600;
int t_min = t_now / 60;
t_now = t_now % 60;
// format time in xxx:xx:xx
sprintf( buffer, "%d:%.2d:%.2d", t_hour, t_min, t_now);
curr_time = buffer;
return curr_time;
}