Skip to content

Commit

Permalink
Logfile location configurability.
Browse files Browse the repository at this point in the history
Fixes #555
  • Loading branch information
jshort committed Dec 22, 2022
1 parent 76656cd commit c9987d2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 29 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ jobs:
- run:
name: Kill server
command: sudo pkill etserver
- run:
name: Debug info/logs if failed
when: always
command: ls -la /tmp/et*; sudo awk 'FNR==1 {print "XXXXXX", FILENAME, "XXXXXX"}{print}' /tmp/et*.log

connect_with_jumphost:
machine:
Expand Down Expand Up @@ -84,6 +88,10 @@ jobs:
- run:
name: Kill servers
command: sudo pkill etserver
- run:
name: Debug info/logs if failed
when: always
command: ls -la /tmp/et*; sudo awk 'FNR==1 {print "XXXXXX", FILENAME, "XXXXXX"}{print}' /tmp/et*.log

workflows:
version: 2
Expand Down
1 change: 1 addition & 0 deletions etc/et.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ port = 2022
verbose = 0
silent = 0
logsize = 20971520
logdirectory = /tmp
telemetry = 1
33 changes: 21 additions & 12 deletions src/base/LogHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@ void LogHandler::setupLogFiles(el::Configurations *defaultConf,
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%Y-%m-%d_%H-%M-%S", timeinfo);
string current_time(buffer);
string logFilename = path + "/" + filenamePrefix + "-" + current_time;
string stderrFilename =
path + "/" + filenamePrefix + "-stderr-" + current_time;
string logFilename = filenamePrefix + "-" + current_time;
string stderrFilename = filenamePrefix + "-stderr-" + current_time;
if (appendPid) {
string pid = std::to_string(getpid());
logFilename.append("_" + pid);
stderrFilename.append("_" + pid);
}
logFilename.append(".log");
stderrFilename.append(".log");
createLogFile(logFilename.c_str());
string fullFname = createLogFile(path, logFilename);

// Enable strict log file size check
el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
defaultConf->setGlobally(el::ConfigurationType::Filename, logFilename);
defaultConf->setGlobally(el::ConfigurationType::Filename, fullFname);
defaultConf->setGlobally(el::ConfigurationType::ToFile, "true");
defaultConf->setGlobally(el::ConfigurationType::MaxLogFileSize, maxlogsize);

Expand All @@ -60,7 +59,7 @@ void LogHandler::setupLogFiles(el::Configurations *defaultConf,
}

if (redirectStderrToFile) {
stderrToFile(stderrFilename);
stderrToFile(path, stderrFilename);
}
}

Expand All @@ -82,18 +81,28 @@ void LogHandler::setupStdoutLogger() {
el::Loggers::reconfigureLogger(stdoutLogger, stdoutConf);
}

void LogHandler::createLogFile(const string &filename) {
string LogHandler::createLogFile(const string &path, const string &filename) {
string fullFname = path + "/" + filename;
try {
fs::create_directories(path);
} catch (const fs::filesystem_error &fse) {
CLOG(ERROR, "stdout") << "Cannot create logfile directory: " << fse.what()
<< endl;
exit(1);
}
#ifdef WIN32
// O_NOFOLLOW does not exist on windows
FATAL_FAIL(::open(filename.c_str(), O_EXCL | O_CREAT, 0600));
FATAL_FAIL(::open(fullFname.c_str(), O_EXCL | O_CREAT, 0600));
#else
FATAL_FAIL(::open(filename.c_str(), O_NOFOLLOW | O_EXCL | O_CREAT, 0600));
FATAL_FAIL(::open(fullFname.c_str(), O_NOFOLLOW | O_EXCL | O_CREAT, 0600));
#endif
return fullFname;
}

void LogHandler::stderrToFile(const string &stderrFilename) {
createLogFile(stderrFilename.c_str());
FILE *stderr_stream = freopen(stderrFilename.c_str(), "w", stderr);
void LogHandler::stderrToFile(const string &path,
const string &stderrFilename) {
string fullFname = createLogFile(path, stderrFilename);
FILE *stderr_stream = freopen(fullFname.c_str(), "w", stderr);
if (!stderr_stream) {
STFATAL << "Invalid filename " << stderrFilename;
}
Expand Down
4 changes: 2 additions & 2 deletions src/base/LogHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class LogHandler {
static void setupStdoutLogger();

private:
static void stderrToFile(const string &stderrFilename);
static void createLogFile(const string &filename);
static void stderrToFile(const string &path, const string &stderrFilename);
static string createLogFile(const string &path, const string &filename);
};
} // namespace et
#endif // __ET_LOG_HANDLER__
16 changes: 9 additions & 7 deletions src/terminal/TerminalClientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ int main(int argc, char** argv) {
("v,verbose", "Enable verbose logging",
cxxopts::value<int>()->default_value("0")) //
("k,keepalive", "Client keepalive duration in seconds",
cxxopts::value<int>()) //
("logtostdout", "Write log to stdout") //
("silent", "Disable logging") //
("N,no-terminal", "Do not create a terminal") //
("f,forward-ssh-agent", "Forward ssh-agent socket") //
cxxopts::value<int>()) //
("l,logdir", "Base directory for log files.",
cxxopts::value<std::string>()->default_value(tmpDir)) //
("logtostdout", "Write log to stdout") //
("silent", "Disable logging") //
("N,no-terminal", "Do not create a terminal") //
("f,forward-ssh-agent", "Forward ssh-agent socket") //
("ssh-socket", "The ssh-agent socket to forward",
cxxopts::value<std::string>()) //
("telemetry",
Expand Down Expand Up @@ -124,8 +126,8 @@ int main(int argc, char** argv) {
defaultConf.setGlobally(el::ConfigurationType::Enabled, "false");
}

LogHandler::setupLogFiles(&defaultConf, tmpDir, "etclient",
result.count("logtostdout"),
LogHandler::setupLogFiles(&defaultConf, result["logdir"].as<string>(),
"etclient", result.count("logtostdout"),
!result.count("logtostdout"));

el::Loggers::reconfigureLogger("default", defaultConf);
Expand Down
8 changes: 5 additions & 3 deletions src/terminal/TerminalMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ int main(int argc, char** argv) {
// Not used by etterminal but easylogging uses this flag under the hood
("v,verbose", "Enable verbose logging",
cxxopts::value<int>()->default_value("0")) //
("logtostdout", "Write log to stdout") //
("l,logdir", "Base directory for log files.",
cxxopts::value<std::string>()->default_value(GetTempDirectory())) //
("logtostdout", "Write log to stdout") //
("serverfifo",
"If set, connects to the etserver instance listening on the matching "
"fifo name", //
Expand Down Expand Up @@ -138,7 +140,7 @@ int main(int argc, char** argv) {
string username = string(ssh_get_local_username());
if (result.count("jump")) {
// etserver with --jump cannot write to the default log file(root)
LogHandler::setupLogFiles(&defaultConf, GetTempDirectory(),
LogHandler::setupLogFiles(&defaultConf, result["logdir"].as<string>(),
("etjump-" + username + "-" + id),
result.count("logtostdout"), false);
// Reconfigure default logger to apply settings above
Expand Down Expand Up @@ -167,7 +169,7 @@ int main(int argc, char** argv) {
}

// etserver with --idpasskey cannot write to the default log file(root)
LogHandler::setupLogFiles(&defaultConf, GetTempDirectory(),
LogHandler::setupLogFiles(&defaultConf, result["logdir"].as<string>(),
("etterminal-" + username + "-" + id),
result.count("logtostdout"), false);

Expand Down
22 changes: 17 additions & 5 deletions src/terminal/TerminalServerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ int main(int argc, char **argv) {
("daemon", "Daemonize the server") //
("cfgfile", "Location of the config file",
cxxopts::value<std::string>()->default_value("")) //
("logtostdout", "log to stdout") //
("l,logdir", "Base directory for log files.",
cxxopts::value<std::string>()) //
("logtostdout", "log to stdout") //
("pidfile", "Location of the pid file",
cxxopts::value<std::string>()->default_value(
"/var/run/etserver.pid")) //
Expand Down Expand Up @@ -77,6 +79,7 @@ int main(int argc, char **argv) {

int port = 0;
string bindIp = "";
string logDirectory = GetTempDirectory();
bool telemetry = true;
if (result.count("cfgfile")) {
// Load the config file
Expand Down Expand Up @@ -120,13 +123,19 @@ int main(int argc, char **argv) {
if (silent && atoi(silent) != 0) {
defaultConf.setGlobally(el::ConfigurationType::Enabled, "false");
}

// read log file size limit
const char *logsize = ini.GetValue("Debug", "logsize", NULL);
if (logsize && atoi(logsize) != 0) {
// make sure maxlogsize is a string of int value
maxlogsize = string(logsize);
}

// log file directory (TODO path validation and trailing slash cleanup)
const char *logdir = ini.GetValue("Debug", "logdirectory", NULL);
if (logdir) {
logDirectory = string(logdir);
}
} else {
STFATAL << "Invalid config file: " << cfgfilename;
}
Expand All @@ -149,6 +158,10 @@ int main(int argc, char **argv) {
telemetry = result["telemetry"].as<bool>();
}

if (result.count("logdir")) {
logDirectory = result["logdir"].as<string>();
}

GOOGLE_PROTOBUF_VERIFY_VERSION;
srand(1);

Expand All @@ -157,10 +170,9 @@ int main(int argc, char **argv) {
}

// Set log file for etserver process here.
LogHandler::setupLogFiles(&defaultConf, GetTempDirectory(), "etserver",
result.count("logtostdout"),
!result.count("logtostdout"),
true /* appendPid */, maxlogsize);
LogHandler::setupLogFiles(
&defaultConf, logDirectory, "etserver", result.count("logtostdout"),
!result.count("logtostdout"), true /* appendPid */, maxlogsize);
// Reconfigure default logger to apply settings above
el::Loggers::reconfigureLogger("default", defaultConf);
// set thread name
Expand Down

0 comments on commit c9987d2

Please sign in to comment.