diff --git a/libstuff/libstuff.cpp b/libstuff/libstuff.cpp index 3f2de0dd1..ed76e5a01 100644 --- a/libstuff/libstuff.cpp +++ b/libstuff/libstuff.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "libstuff.h" #include @@ -92,9 +93,19 @@ atomic SLogSocketCurrentOffset(0); struct sockaddr_un SLogSocketAddr; atomic_flag SLogSocketsInitialized = ATOMIC_FLAG_INIT; +bool g_isSyslog = false; + // Set to `syslog` or `SSyslogSocketDirect`. atomic SSyslogFunc = &syslog; +void bclog(int priority, const char *fmt, const char* msg ) { + cout << msg; +} + +void SLogSetType(bool isSyslog) { + g_isSyslog = isSyslog; +} + void SInitialize(string threadName, const char* processName) { // This is not really thread safe. It's guaranteed to run only once, because of the atomic flag, but it's not // guaranteed that a second caller to `SInitialize` will wait until this block has completed before attempting to diff --git a/libstuff/libstuff.h b/libstuff/libstuff.h index e46208b9e..158f956dd 100644 --- a/libstuff/libstuff.h +++ b/libstuff/libstuff.h @@ -219,6 +219,9 @@ void STerminateHandler(void); extern atomic _g_SLogMask; void SLogLevel(int level); +void bclog(int priority, const char *fmt, const char* msg ); +void SLogSetType(bool isSyslog); + // Stack trace logging void SLogStackTrace(); @@ -227,6 +230,7 @@ void SSyslogSocketDirect(int priority, const char* format, ...); // Atomic pointer to the syslog function that we'll actually use. Easy to change to `syslog` or `SSyslogSocketDirect`. extern atomic SSyslogFunc; +extern bool g_isSyslog; // **NOTE: rsyslog default max line size is 8k bytes. We split on 7k byte boundaries in order to fit the syslog line prefix and the expanded \r\n to #015#012 #define SWHEREAMI SThreadLogPrefix + "(" + basename((char*)__FILE__) + ":" + SToStr(__LINE__) + ") " + __FUNCTION__ + " [" + SThreadLogName + "] " @@ -238,7 +242,10 @@ extern atomic SSyslogFunc; const string s = __out.str(); \ const string prefix = SWHEREAMI; \ for (size_t i = 0; i < s.size(); i += 7168) { \ - (*SSyslogFunc)(_PRI_, "%s", (prefix + s.substr(i, 7168)).c_str()); \ + if (g_isSyslog) \ + SSyslogFunc(_PRI_, "%s", (prefix + s.substr(i, 7168)).c_str()); \ + else \ + bclog(_PRI_, "%s", (prefix + s.substr(i, 7168)).c_str()); \ } \ } \ } while (false) diff --git a/main.cpp b/main.cpp index 09167af58..599511012 100644 --- a/main.cpp +++ b/main.cpp @@ -143,6 +143,7 @@ set loadPlugins(SData& args) { int main(int argc, char* argv[]) { // Process the command line SData args = SParseCommandLine(argc, argv); + SLogSetType(false); if (args.empty()) { // It's valid to run bedrock with no parameters provided, but unusual // -- let's provide some help just in case @@ -174,6 +175,7 @@ int main(int argc, char* argv[]) { // Fork if requested if (args.isSet("-fork")) { // Do the fork + SLogSetType(true); int pid = fork(); SASSERT(pid >= 0); if (pid > 0) {