Aggie Student Toolkit — Load Balancer
CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Toggle main menu visibility
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1
6
7
#include "
Logger.h
"
8
9
#include <cstdio>
10
#include <iomanip>
11
#include <iostream>
12
#include <sstream>
13
#include <unistd.h>
14
15
namespace
{
16
const
char
*
const
kReset =
"\033[0m"
;
17
}
18
19
Logger::Logger
(
bool
verbose
,
bool
useColor)
20
: verbose_(
verbose
), useColor_(useColor && isatty(fileno(stdout)) != 0) {}
21
22
bool
Logger::openEventFile
(
const
std::string& path) {
23
eventFile_.open(path);
24
return
eventFile_.is_open();
25
}
26
27
bool
Logger::verbose
()
const
{
28
return
verbose_;
29
}
30
31
const
char
* Logger::label(
LogCategory
category) {
32
switch
(category) {
33
case
LogCategory::Arrival
:
return
"ARRIVE "
;
34
case
LogCategory::Assign
:
return
"ASSIGN "
;
35
case
LogCategory::Complete
:
return
"DONE "
;
36
case
LogCategory::Reject
:
return
"REJECT "
;
37
case
LogCategory::Discard
:
return
"DISCARD"
;
38
case
LogCategory::ScaleUp
:
return
"SCALE+ "
;
39
case
LogCategory::ScaleDown
:
return
"SCALE- "
;
40
case
LogCategory::Status
:
return
"STATUS "
;
41
case
LogCategory::Info
:
return
"INFO "
;
42
}
43
return
""
;
44
}
45
46
const
char
* Logger::color(
LogCategory
category) {
47
switch
(category) {
48
case
LogCategory::Arrival
:
return
"\033[34m"
;
// blue
49
case
LogCategory::Assign
:
return
"\033[36m"
;
// cyan
50
case
LogCategory::Complete
:
return
"\033[32m"
;
// green
51
case
LogCategory::Reject
:
return
"\033[1;31m"
;
// bold red
52
case
LogCategory::Discard
:
return
"\033[31m"
;
// red
53
case
LogCategory::ScaleUp
:
return
"\033[1;33m"
;
// bold yellow
54
case
LogCategory::ScaleDown
:
return
"\033[1;35m"
;
// bold magenta
55
case
LogCategory::Status
:
return
"\033[1;37m"
;
// bold white
56
case
LogCategory::Info
:
return
"\033[37m"
;
// white
57
}
58
return
""
;
59
}
60
61
std::string
Logger::colorize
(
LogCategory
category,
const
std::string& text)
const
{
62
if
(!useColor_) {
63
return
text;
64
}
65
return
std::string(color(category)) + text + kReset;
66
}
67
68
void
Logger::log
(
LogCategory
category,
unsigned
long
cycle,
const
std::string& source,
69
const
std::string& message) {
70
const
bool
alwaysShown = category ==
LogCategory::Status
|| category ==
LogCategory::Info
;
71
if
(!verbose_ && !alwaysShown) {
72
return
;
73
}
74
std::ostringstream line;
75
line <<
"[t="
<< std::setw(6) << std::setfill(
'0'
) << cycle <<
"] ["
<< source <<
"] "
76
<< label(category) <<
" "
<< message;
77
std::cout <<
colorize
(category, line.str()) <<
'\n'
;
78
if
(eventFile_.is_open()) {
79
eventFile_ << line.str() <<
'\n'
;
80
}
81
}
Logger.h
Declares the Logger class that prints colored, mode-aware simulation events.
LogCategory
LogCategory
Kind of event being logged; selects the color and whether quiet mode shows it.
Definition
Logger.h:16
LogCategory::Assign
@ Assign
A queued request was dispatched to a server.
Definition
Logger.h:18
LogCategory::Info
@ Info
General information such as start-up messages.
Definition
Logger.h:25
LogCategory::Arrival
@ Arrival
A request entered the queue.
Definition
Logger.h:17
LogCategory::ScaleDown
@ ScaleDown
A server was removed or marked for retirement.
Definition
Logger.h:23
LogCategory::Complete
@ Complete
A server finished a request.
Definition
Logger.h:19
LogCategory::Discard
@ Discard
The queue was full and a request was dropped.
Definition
Logger.h:21
LogCategory::Reject
@ Reject
The firewall blocked a request.
Definition
Logger.h:20
LogCategory::Status
@ Status
Periodic queue and server status line.
Definition
Logger.h:24
LogCategory::ScaleUp
@ ScaleUp
A server was added or returned to service.
Definition
Logger.h:22
Logger::openEventFile
bool openEventFile(const std::string &path)
Copies events to a file in addition to the console.
Definition
Logger.cpp:22
Logger::verbose
bool verbose() const
Definition
Logger.cpp:27
Logger::log
void log(LogCategory category, unsigned long cycle, const std::string &source, const std::string &message)
Logs one event.
Definition
Logger.cpp:68
Logger::Logger
Logger(bool verbose, bool useColor)
Creates a logger.
Definition
Logger.cpp:19
Logger::colorize
std::string colorize(LogCategory category, const std::string &text) const
Wraps text in an ANSI color for console output when colors are enabled.
Definition
Logger.cpp:61
Logger.cpp
Generated by
1.18.0