Skip to content

Commit 33081c6

Browse files
committed
add output in permanent file
1 parent ab7f288 commit 33081c6

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/utils/LogFileSink.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@
66

77
static const std::string file_name_time_formatted = "%Y%m%d-%H%M%S";
88

9-
LogFileSink::LogFileSink(const std::string &log_prefix, const std::string &log_directory, const bool show_microseconds_)
10-
: _log_file_with_path(log_directory), _log_prefix_backup(log_prefix), _outptr(new std::ofstream), show_microseconds(show_microseconds_)
9+
LogFileSink::LogFileSink(const std::string &log_prefix, const std::string &log_directory, const bool show_microseconds_,
10+
const bool standing_name_)
11+
: _log_file_with_path(log_directory), _log_prefix_backup(log_prefix), _outptr(new std::ofstream),
12+
show_microseconds(show_microseconds_), standing_name(standing_name_)
1113
{
1214
_log_prefix_backup = prefixSanityFix(log_prefix);
1315
if (!isValidFilename(_log_prefix_backup)) {
1416
std::cerr << "LogFileSink: forced abort due to illegal log prefix [" << log_prefix << "]" << std::endl;
1517
abort();
1618
}
1719

18-
std::string file_name = createLogFileName(_log_prefix_backup);
20+
std::string file_name;
21+
if (standing_name_) {
22+
file_name = _log_prefix_backup + ".log";
23+
} else {
24+
file_name = createLogFileName(_log_prefix_backup);
25+
}
1926
_log_file_with_path = pathSanityFix(_log_file_with_path, file_name);
2027
_outptr = createLogFile(_log_file_with_path);
2128

@@ -165,7 +172,11 @@ std::string LogFileSink::createLogFileName(const std::string &verified_prefix)
165172
bool LogFileSink::openLogFile(const std::string &complete_file_with_path, std::ofstream &outstream)
166173
{
167174
std::ios_base::openmode mode = std::ios_base::out; // for clarity: it's really overkill since it's an ofstream
168-
mode |= std::ios_base::trunc;
175+
if (standing_name){
176+
mode |= std::ios_base::app;
177+
} else {
178+
mode |= std::ios_base::trunc;
179+
}
169180
outstream.open(complete_file_with_path, mode);
170181
if (!outstream.is_open()) {
171182
std::ostringstream ss_error;

src/utils/LogFileSink.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ using namespace g3;
1111
class LogFileSink
1212
{
1313
public:
14-
LogFileSink(const std::string &log_prefix, const std::string &log_directory, const bool show_microseconds);
14+
LogFileSink(const std::string &log_prefix, const std::string &log_directory,
15+
const bool show_microseconds = false, const bool standing_name = false);
1516
virtual ~LogFileSink();
1617

1718
void fileWrite(const LogMessageMover message);
@@ -21,6 +22,7 @@ class LogFileSink
2122
std::string _log_prefix_backup; // needed in case of future log file changes of directory
2223
std::unique_ptr <std::ofstream> _outptr;
2324
bool show_microseconds;
25+
bool standing_name;
2426

2527
std::ofstream &filestream()
2628
{

tools/colorer/ConsoleToolsRunner.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct setting {
2727
std::string log_file_dir = "./";
2828
std::string log_level = "INFO";
2929
bool enable_logging = false;
30+
bool standing_logfile = false;
3031
int profile_loops = 1;
3132
bool line_numbers = false;
3233
bool copyright = true;
@@ -200,6 +201,10 @@ void readArgs(int argc, char* argv[])
200201
settings.enable_logging = true;
201202
continue;
202203
}
204+
if (argv[i][1] == 'e' && argv[i][2] == 's') {
205+
settings.standing_logfile = true;
206+
continue;
207+
}
203208
if (argv[i][1]) {
204209
fprintf(stderr, "WARNING: unknown option '-%s'\n", argv[i] + 1);
205210
}
@@ -239,6 +244,7 @@ void printError()
239244
" -eh<name> Log file name prefix\n"
240245
" -ed<name> Log file directory\n"
241246
" -el<name> Log level (DEBUG, INFO, WARNING, ERROR, ERROR_F, FATAL)\n"
247+
" -es Standing log file name, without date"
242248
);
243249
};
244250

@@ -319,7 +325,7 @@ int workIt()
319325
printError();
320326
break;
321327
}
322-
} catch (Exception e) {
328+
} catch (Exception &e) {
323329
LOG(ERROR) << e.what();
324330
fprintf(stderr, "%s", e.what());
325331
return -1;
@@ -336,7 +342,7 @@ int main(int argc, char* argv[])
336342
std::unique_ptr<LogWorker> log_worker;
337343
if (settings.enable_logging) {
338344
log_worker = std::move(g3::LogWorker::createLogWorker());
339-
auto handle = log_worker->addSink(std2::make_unique<LogFileSink>(settings.log_file_prefix, settings.log_file_dir, false), &LogFileSink::fileWrite);
345+
auto handle = log_worker->addSink(std2::make_unique<LogFileSink>(settings.log_file_prefix, settings.log_file_dir, false, settings.standing_logfile), &LogFileSink::fileWrite);
340346
g3::only_change_at_initialization::setLogLevel(settings.log_level);
341347
g3::initializeLogging(log_worker.get());
342348
}

0 commit comments

Comments
 (0)