-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
69 lines (60 loc) · 1.89 KB
/
Logger.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
//-----------------------------------------------------------
// File : Logger.cpp
// Class: CEN 3078, Spring 2023
// Devl : Katarya Johnson-Williams
// Desc : Implementation of a Logger class object
//---------------------------------------------------------
#include <iostream> // console I/O
#include <fstream> // for file writing
#include <chrono> // for timestamps
#include "Logger.h"
// Constructors
//---------------------------------------------------------
ErrorSeverity Logger::severity = TRACE;
const char* Logger::filepath = "log.txt";
// Methods
//---------------------------------------------------------
/**
* dynamically update the severity of the log
* @param newSeverity - new severity value
*/
void Logger::setSeverity(ErrorSeverity newSeverity) {
severity = newSeverity;
}
/**
* create a log of an error with a message
* @param type - level of severity
* @param message - to be included with error
*/
void Logger::log(ErrorSeverity type, const char* message) {
// change label based on type
if (type >= severity) {
std::ofstream FILE(filepath, std::ios_base::app);
// add timestamp to file
auto time = std::chrono::system_clock::now();
std::time_t timestamp = std::chrono::system_clock::to_time_t(time);
FILE << std::ctime(×tamp);
switch (type) {
case TRACE:
FILE << " TRACE: ";
break;
case DEBUG:
FILE << " DEBUG: ";
break;
case INFO:
FILE << " INFO: ";
break;
case WARN:
FILE << " WARN: ";
break;
case ERROR:
FILE << " ERROR: ";
break;
case CRITICAL:
FILE << " CRITICAL: ";
}
// add error message to log.txt
FILE << message << "\n";
FILE.close();
}
}