-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
121 lines (106 loc) · 2.53 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
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
//
// Logger.cpp
//
// Created by Nordstrom, Steve (Faculty - nordstrosg) on 9/5/19.
// Updated on 1/15/2020.
// Copyright © 2020 Nordstrom, Steve (Faculty - nordstrosg). All rights reserved.
//
#include "Logger.hpp"
/// <summary>
/// Constructor to start a logger using a specified file name
/// </summary>
/// <param name="fileName">A string containing path for file</param>
Logger::Logger(std::string fileName)
{
//store the name of the file for later
logFileName = fileName;
//turn the console on by default
consoleEnabled = true;
//no need to open file yet
fileEnabled = false;
}
/// <summary>
/// Destructor to tear down a logger
/// </summary>
Logger::~Logger()
{
// close file (if it is open)
if(myFile.is_open()){
myFile.close();
fileEnabled = false;
}
}
/// <summary>
/// Method to log some string data
/// </summary>
/// <param name="line">A string to put as a line of text in the log</param>
/// <returns>True if line was logged properly, false otherwise</returns>
bool Logger::Log(std::string line)
{
if(fileEnabled){
//check to see if file is open, open if not and log line
myFile << line << std::endl;
}
// Log to each target if enabled.
if(consoleEnabled){
std::cout << line << std::endl;
}
return true;
}
/// <summary>
/// Control method to turn on file-based logging
/// </summary>
void Logger::EnableFileLogging()
{
if(!myFile.is_open())
{
myFile.open(logFileName);
fileEnabled = true;
}
else{
fileEnabled = true;
}
}
/// <summary>
/// Control method to turn off file-based logging
/// </summary>
void Logger::DisableFileLogging()
{
fileEnabled = false;
}
/// <summary>
/// Control method to turn on console-based logging
/// </summary>
void Logger::EnableConsoleLogging()
{
consoleEnabled = true;
}
/// <summary>
/// Control method to turn off console-based logging
/// </summary>
void Logger::DisableConsoleLogging()
{
consoleEnabled = false;
}
/// <summary>
/// String property to view details of the implementation
/// </summary>
std::string Logger::GetInfo()
{
// return the version number of your Logger
return "Logger V 0.0.1";
}
/// <summary>
/// Boolean property which reflects if the log is currently logging to a file
/// </summary>
bool Logger::IsFileLogEnabled()
{
return fileEnabled;
}
/// <summary>
/// Boolean property which reflects if the log is currently logging to the console
/// </summary>
bool Logger::IsConsoleLogEnabled()
{
return consoleEnabled;
}