Skip to content

Commit 5122b01

Browse files
committed
Print log level in terminal and file
1 parent 6c63eef commit 5122b01

File tree

7 files changed

+57
-29
lines changed

7 files changed

+57
-29
lines changed

.vscode/settings.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@
6767
"stop_token": "cpp",
6868
"thread": "cpp",
6969
"variant": "cpp",
70-
"queue": "cpp"
70+
"queue": "cpp",
71+
"charconv": "cpp",
72+
"codecvt": "cpp",
73+
"format": "cpp",
74+
"stdfloat": "cpp",
75+
"text_encoding": "cpp",
76+
"cfenv": "cpp",
77+
"valarray": "cpp"
7178
},
72-
"cmake.configureOnOpen": false
79+
"C_Cpp.clang_format_fallbackStyle": "Google",
7380
}

include/output/Log.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ class Log {
4141
static log_level_t _log_level;
4242

4343
std::string _path;
44-
std::ostream& _terminal;
44+
std::ostream &_terminal;
4545
std::ofstream _file;
4646

4747
static std::string _timeFormat;
4848
static std::string _dateFormat;
4949

50-
bool _fileSet; // True once file has been set
51-
bool _initialized; // Enables/Disables logfile functionality
50+
bool _fileSet;
51+
bool _logToFile;
5252

53-
bool _error; // True if an error occured while writing to the log file
53+
bool _logFileWriteError;
5454

5555
public:
56-
Log(std::ostream& terminal);
56+
Log(std::ostream &terminal);
5757
~Log();
5858

5959
void init(std::string path);
@@ -62,16 +62,17 @@ class Log {
6262
static void setLogToTerminal(bool log, bool overwrite = false);
6363
static void setLevel(log_level_t level, bool overwrite = false);
6464
void setFile(std::string path, bool overwrite = false);
65-
void setInitialized(bool intialized);
65+
void setLogToFile(bool logToFile);
6666

6767
// Getters
6868
static log_level_t getLevel();
69-
bool getInitialized();
69+
static std::string getLevelName(log_level_t level);
70+
bool getLogToFile();
7071

7172
// Writes a message to the log file and if enabled also to the _terminal
7273
// stream
7374
// @exception No custom exceptions
74-
void write(std::string const& msg, log_level_t level,
75+
void write(std::string const &msg, log_level_t level,
7576
std::string color = RESET);
7677

7778
private:

src/config/Init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ void Init::initLogDefaults(Context& context) {
3939
accessLog_g.setFile(context.getDirective("access_log")[0][0]);
4040
if (context.exists("error_log"))
4141
errorLog_g.setFile(context.getDirective("error_log")[0][0]);
42-
accessLog_g.setInitialized(true);
43-
errorLog_g.setInitialized(true);
42+
accessLog_g.setLogToFile(true);
43+
errorLog_g.setLogToFile(true);
4444
}
4545

4646
void Init::initMimeTypes(Context& context) {

src/config/argument.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ arg_state_t setAccessLog(const std::list<std::string> &values) {
6969
static bool set = false;
7070
if (set) return FLAG_DUPLICATE;
7171
accessLog_g.setFile(values.front());
72-
accessLog_g.setInitialized(true);
72+
accessLog_g.setLogToFile(true);
7373
set = true;
7474
return SUCCESS;
7575
}
@@ -78,7 +78,7 @@ arg_state_t setErrorLog(const std::list<std::string> &values) {
7878
static bool set = false;
7979
if (set) return FLAG_DUPLICATE;
8080
errorLog_g.setFile(values.front());
81-
errorLog_g.setInitialized(true);
81+
errorLog_g.setLogToFile(true);
8282
set = true;
8383
return SUCCESS;
8484
}

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main(int argc, char** argv) {
4848
}
4949
} catch (const std::exception& e) {
5050
try {
51-
if (errorLog_g.getInitialized() == false)
51+
if (errorLog_g.getLogToFile() == false)
5252
Log::setLogToTerminal(true, true);
5353
errorLog_g.write(e.what(), ERROR);
5454
printHelp(PRINT);

src/output/Log.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,70 @@ void Log::init(std::string path) {
1919

2020
void Log::setLogToTerminal(bool log, bool overwrite) {
2121
static bool initialized = false;
22-
if (overwrite || !initialized) {
22+
if (overwrite || initialized == false) {
2323
_log_to_terminal = log;
2424
initialized = true;
2525
}
2626
}
2727

2828
void Log::setLevel(log_level_t level, bool overwrite) {
2929
static bool initialized = false;
30-
if (overwrite || !initialized) {
30+
if (overwrite || initialized == false) {
3131
_log_level = level;
3232
initialized = true;
3333
}
3434
}
3535

3636
void Log::setFile(std::string path, bool overwrite) {
37-
if (overwrite || !_fileSet) {
37+
if (overwrite || _fileSet == false) {
3838
File(path).createDirPath();
3939
_path = path;
4040
if (_file.is_open()) _file.close();
4141
_file.open(path.c_str(), std::ios_base::app);
42-
if (_file.good()) _error = false;
42+
if (_file.good()) _logFileWriteError = false;
4343
_fileSet = true;
4444
}
4545
}
4646

47-
void Log::setInitialized(bool initialized) { _initialized = initialized; }
47+
void Log::setLogToFile(bool logToFile) { _logToFile = logToFile; }
4848

4949
log_level_t Log::getLevel() { return _log_level; }
5050

51-
bool Log::getInitialized() { return _initialized; }
51+
static std::string getLevelString(log_level_t level) {
52+
switch (level)
53+
{
54+
case ERROR:
55+
return "Error ";
56+
case WARNING:
57+
return "Warning";
58+
case INFO:
59+
return "Info ";
60+
case DEBUG:
61+
return "Debug ";
62+
case VERBOSE:
63+
return "Verbose";
64+
65+
default:
66+
return "Unknown";
67+
}
68+
}
69+
70+
bool Log::getLogToFile() { return _logToFile; }
5271

5372
void Log::write(std::string const &msg, log_level_t level, std::string color) {
5473
if (level > _log_level) return;
55-
std::string timeStamp = "[" + getTime(_dateFormat + " " + _timeFormat) + "] ";
74+
std::string timeStamp = "[" + getTime(_dateFormat + " " + _timeFormat) + "]";
75+
std::string levelString = "[" + getLevelString(level) + "] ";
5676
if (_log_to_terminal) {
5777
if (color == RESET) color = getLevelColor(level);
58-
_terminal << timeStamp << color << msg << RESET << std::endl;
78+
_terminal << timeStamp << color << levelString << msg << RESET << std::endl;
5979
}
60-
if (!_initialized) return;
61-
_file << timeStamp << msg << std::endl;
62-
if (_file.good() || _error) return;
80+
if (getLogToFile() == false) return;
81+
_file << timeStamp << levelString << msg << std::endl;
82+
if (_file.good() || _logFileWriteError == true) return;
6383
std::cerr << timeStamp << BRIGHT_RED << "ERROR: " << RESET
6484
<< "Unable to write to log file: " << _path << std::endl;
65-
_error = true;
85+
_logFileWriteError = true;
6686
}
6787

6888
std::string Log::getLevelColor(log_level_t level) {

src/poll/AConnection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ void AConnection::runCGI(std::string program,
435435
}
436436
if (_cgiPid == 0) {
437437
Log::setLogToTerminal(false, true);
438-
accessLog_g.setInitialized(false);
439-
errorLog_g.setInitialized(false);
438+
accessLog_g.setLogToFile(false);
439+
errorLog_g.setLogToFile(false);
440440
Poll::cleanUp();
441441
close(pipeInArray[0]);
442442
close(pipeOutArray[1]);

0 commit comments

Comments
 (0)