Skip to content

Commit f74bf53

Browse files
aszligoxij
authored andcommitted
Treat logging to /dev/stderr specially, log there by default
... mainly for integration with systemd. Adapted from NixOS/nixpkgs@83e1400
1 parent cbeabbd commit f74bf53

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

cfg.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Cfg::Cfg() : currentSession(-1)
5858
options.insert(option("auto_login", "no"));
5959
options.insert(option("current_theme", "default"));
6060
options.insert(option("lockfile", "/var/run/slim.lock"));
61-
options.insert(option("logfile", "/var/log/slim.log"));
61+
options.insert(option("logfile", "/dev/stderr"));
6262
options.insert(option("authfile", "/var/run/slim.auth"));
6363
options.insert(option("shutdown_msg", "The system is halting..."));
6464
options.insert(option("reboot_msg", "The system is rebooting..."));

log.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "log.h"
2+
#include <cstring>
23
#include <iostream>
34

45
bool LogUnit::openLog(const char * filename)
@@ -9,8 +10,12 @@ bool LogUnit::openLog(const char * filename)
910
<< endl;
1011
logFile.close();
1112
}
12-
logFile.open(filename, ios_base::app);
1313

14+
// cerr is the default
15+
if (strcmp(filename, "/dev/stderr") == 0)
16+
return true;
17+
18+
logFile.open(filename, ios_base::app);
1419
return !(logFile.fail());
1520
}
1621

log.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
#endif
1010
#include "const.h"
1111
#include <fstream>
12+
#include <iostream>
1213

1314
using namespace std;
1415

1516
static class LogUnit
1617
{
1718
ofstream logFile;
19+
inline ostream & getStream() { return logFile.is_open() ? logFile : cerr; }
1820

1921
public:
2022
bool openLog(const char * filename);
@@ -24,22 +26,22 @@ static class LogUnit
2426

2527
template <typename Type> LogUnit & operator<<(const Type & text)
2628
{
27-
logFile << text;
28-
logFile.flush();
29+
getStream() << text;
30+
getStream().flush();
2931
return *this;
3032
}
3133

3234
LogUnit & operator<<(ostream & (*fp)(ostream &))
3335
{
34-
logFile << fp;
35-
logFile.flush();
36+
getStream() << fp;
37+
getStream().flush();
3638
return *this;
3739
}
3840

3941
LogUnit & operator<<(ios_base & (*fp)(ios_base &))
4042
{
41-
logFile << fp;
42-
logFile.flush();
43+
getStream() << fp;
44+
getStream().flush();
4345
return *this;
4446
}
4547
} logStream;

0 commit comments

Comments
 (0)