Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for console log #1075

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <sys/un.h>
#include <cxxabi.h>
#include <sys/ioctl.h>
#include <iostream>

#include "libstuff.h"
#include <sys/stat.h>
Expand Down Expand Up @@ -92,9 +93,19 @@ atomic<size_t> SLogSocketCurrentOffset(0);
struct sockaddr_un SLogSocketAddr;
atomic_flag SLogSocketsInitialized = ATOMIC_FLAG_INIT;

bool g_isSyslog = false;

// Set to `syslog` or `SSyslogSocketDirect`.
atomic<void (*)(int priority, const char *format, ...)> 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
Expand Down
9 changes: 8 additions & 1 deletion libstuff/libstuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ void STerminateHandler(void);
extern atomic<int> _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();

Expand All @@ -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<void (*)(int priority, const char *format, ...)> 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 + "] "
Expand All @@ -238,7 +242,10 @@ extern atomic<void (*)(int priority, const char *format, ...)> 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)
Expand Down
2 changes: 2 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ set<string> 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
Expand Down Expand Up @@ -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) {
Expand Down