Skip to content

Commit

Permalink
Merge pull request #118 from emilk/runtime-options
Browse files Browse the repository at this point in the history
Runtime options
  • Loading branch information
emilk authored Sep 29, 2019
2 parents c864ee6 + e4eb0fa commit 2ed007e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 64 deletions.
32 changes: 25 additions & 7 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@


# Functions
## `void init(int& argc, char* argv[], const char* verbosity_flag = "-v")`
## `void init(int& argc, char* argv[], const Options& options = {})`

Should be called from the main thread.
You don't *need* to call this, but if you do you get:
Expand Down Expand Up @@ -416,7 +416,30 @@
That way you can set the default but have the user override it with the `-v` flag.
Note that `-v` does not affect file logging (see `loguru::add_file`).

If you want to use the `-v` flag for something else than Loguru, you can set the `verbosity_flag` parameter to e.g. `"--loguru_verbosity"` or `nullptr`.
You can you something other than the `-v` flag by setting the `verbosity_flag` option.

``` C++
// Runtime options passed to loguru::init
struct Options
{
// This allows you to use something else instead of "-v" via verbosity_flag.
// Set to nullptr to if you don't want Loguru to parse verbosity from the args.'
const char* verbosity_flag = "-v";

// loguru::init will set the name of the calling thread to this.
// If you don't want Loguru to set the name of the main thread,
// set this to nullptr.
// NOTE: on SOME platforms loguru::init will only overwrite the thread name
// if a thread name has not already been set.
// To always set a thread name, use loguru::set_thread_name instead.
const char+ main_thread_name = "main thread";

// Make Loguru try to do unsafe but useful things,
// like printing a stack trace, when catching signals.
// This may lead to bad things like deadlocks in certain situations.
bool unsafe_signal_handler = true;
};
```

## `void shutdown()`
Will call `loguru::remove_all_callbacks()`, thus stopping all logging except to `stderr`.
Expand Down Expand Up @@ -621,11 +644,6 @@
### `LOGURU_REPLACE_GLOG = 0`
Make Loguru mimic GLOG as close as possible, including #defining `LOG`, `CHECK`, `VLOG_IS_ON` etc. Great if you plan to migrate from GLOG to Loguru and don't want to add _S suffixes to all your logging functions (see `LOGURU_WITH_STREAMS`). `LOGURU_REPLACE_GLOG` implies `LOGURU_WITH_STREAMS`.

### `LOGURU_UNSAFE_SIGNAL_HANDLER = 1`
Make Loguru try to do unsafe but useful things,
like printing a stack trace, when catching signals.
This may lead to bad things like deadlocks in certain situations.

### `LOGURU_USE_FMTLIB = 0`
Use fmtlib formatting. See https://github.com/fmtlib/fmt
This will make `loguru.hpp` depend on `<fmt/format.h>`
Expand Down
102 changes: 52 additions & 50 deletions loguru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ namespace loguru
flush();
}

static void install_signal_handlers();
static void install_signal_handlers(bool unsafe_signal_handler);

static void write_hex_digit(std::string& out, unsigned num)
{
Expand Down Expand Up @@ -542,7 +542,7 @@ namespace loguru
#endif
}

void init(int& argc, char* argv[], const char* verbosity_flag)
void init(int& argc, char* argv[], const Options& options)
{
CHECK_GT_F(argc, 0, "Expected proper argc/argv");
CHECK_EQ_F(argv[argc], nullptr, "Expected proper argc/argv");
Expand All @@ -553,8 +553,7 @@ namespace loguru
#define getcwd _getcwd
#endif

if (!getcwd(s_current_dir, sizeof(s_current_dir)))
{
if (!getcwd(s_current_dir, sizeof(s_current_dir))) {
const auto error_text = errno_as_text();
LOG_F(WARNING, "Failed to get current working directory: " LOGURU_FMT(s) "", error_text.c_str());
}
Expand All @@ -567,28 +566,30 @@ namespace loguru
}
}

if (verbosity_flag) {
parse_args(argc, argv, verbosity_flag);
if (options.verbosity_flag) {
parse_args(argc, argv, options.verbosity_flag);
}

#if LOGURU_PTLS_NAMES || LOGURU_WINTHREADS
set_thread_name("main thread");
#elif LOGURU_PTHREADS
char old_thread_name[16] = {0};
auto this_thread = pthread_self();
#if defined(__APPLE__) || defined(__linux__)
pthread_getname_np(this_thread, old_thread_name, sizeof(old_thread_name));
#endif
if (old_thread_name[0] == 0) {
#ifdef __APPLE__
pthread_setname_np("main thread");
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(this_thread, "main thread");
#elif defined(__linux__)
pthread_setname_np(this_thread, "main thread");
if (const auto main_thread_name = options.main_thread_name) {
#if LOGURU_PTLS_NAMES || LOGURU_WINTHREADS
set_thread_name(main_thread_name);
#elif LOGURU_PTHREADS
char old_thread_name[16] = {0};
auto this_thread = pthread_self();
#if defined(__APPLE__) || defined(__linux__)
pthread_getname_np(this_thread, old_thread_name, sizeof(old_thread_name));
#endif
}
#endif // LOGURU_PTHREADS
if (old_thread_name[0] == 0) {
#ifdef __APPLE__
pthread_setname_np(main_thread_name);
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(this_thread, main_thread_name);
#elif defined(__linux__)
pthread_setname_np(this_thread, main_thread_name);
#endif
}
#endif // LOGURU_PTHREADS
}

if (g_stderr_verbosity >= Verbosity_INFO) {
if (g_preamble) {
Expand All @@ -610,7 +611,7 @@ namespace loguru
VLOG_F(g_internal_verbosity, "stderr verbosity: " LOGURU_FMT(d) "", g_stderr_verbosity);
VLOG_F(g_internal_verbosity, "-----------------------------------");

install_signal_handlers();
install_signal_handlers(options.unsafe_signal_handler);

atexit(on_atexit);
}
Expand Down Expand Up @@ -1712,13 +1713,10 @@ namespace loguru

#ifdef _WIN32
namespace loguru {
void install_signal_handlers()
void install_signal_handlers(bool unsafe_signal_handler)
{
#if defined(_MSC_VER)
#pragma message ( "No signal handlers on Win32" )
#else
#warning "No signal handlers on Win32"
#endif
(void)unsafe_signal_handler;
// TODO: implement signal handlers on windows
}
} // namespace loguru

Expand Down Expand Up @@ -1764,6 +1762,8 @@ namespace loguru
kill(getpid(), signal_number);
}

static bool s_unsafe_signal_handler = false;

void signal_handler(int signal_number, siginfo_t*, void*)
{
const char* signal_name = "UNKNOWN SIGNAL";
Expand Down Expand Up @@ -1797,33 +1797,35 @@ namespace loguru

// --------------------------------------------------------------------

#if LOGURU_UNSAFE_SIGNAL_HANDLER
// --------------------------------------------------------------------
/* Now we do unsafe things. This can for example lead to deadlocks if
the signal was triggered from the system's memory management functions
and the code below tries to do allocations.
*/
if (s_unsafe_signal_handler) {
// --------------------------------------------------------------------
/* Now we do unsafe things. This can for example lead to deadlocks if
the signal was triggered from the system's memory management functions
and the code below tries to do allocations.
*/

flush();
char preamble_buff[LOGURU_PREAMBLE_WIDTH];
print_preamble(preamble_buff, sizeof(preamble_buff), Verbosity_FATAL, "", 0);
auto message = Message{Verbosity_FATAL, "", 0, preamble_buff, "", "Signal: ", signal_name};
try {
log_message(1, message, false, false);
} catch (...) {
// This can happed due to s_fatal_handler.
write_to_stderr("Exception caught and ignored by Loguru signal handler.\n");
}
flush();
flush();
char preamble_buff[LOGURU_PREAMBLE_WIDTH];
print_preamble(preamble_buff, sizeof(preamble_buff), Verbosity_FATAL, "", 0);
auto message = Message{Verbosity_FATAL, "", 0, preamble_buff, "", "Signal: ", signal_name};
try {
log_message(1, message, false, false);
} catch (...) {
// This can happed due to s_fatal_handler.
write_to_stderr("Exception caught and ignored by Loguru signal handler.\n");
}
flush();

// --------------------------------------------------------------------
#endif // LOGURU_UNSAFE_SIGNAL_HANDLER
// --------------------------------------------------------------------
}

call_default_signal_handler(signal_number);
}

void install_signal_handlers()
void install_signal_handlers(bool unsafe_signal_handler)
{
s_unsafe_signal_handler = unsafe_signal_handler;

struct sigaction sig_action;
memset(&sig_action, 0, sizeof(sig_action));
sigemptyset(&sig_action.sa_mask);
Expand Down
35 changes: 28 additions & 7 deletions loguru.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Website: www.ilikebigbits.com
* Version 1.8.0 - 2018-04-23 - Shorten long file names to keep preamble fixed width
* Version 1.9.0 - 2018-09-22 - Adjust terminal colors, add LOGURU_VERBOSE_SCOPE_ENDINGS, add LOGURU_SCOPE_TIME_PRECISION, add named log levels
* Version 2.0.0 - 2018-09-22 - Split loguru.hpp into loguru.hpp and loguru.cpp
* Version 2.1.0 - 2019-09-23 - Update fmtlib = add option to loguru::init to NOT set main thread name.
# Compiling
Just include <loguru.hpp> where you want to use Loguru.
Expand All @@ -78,7 +79,7 @@ Website: www.ilikebigbits.com
*/

#if defined(LOGURU_IMPLEMENTATION)
#warning "You are defining LOGURU_IMPLEMENTATION. This is for older versions of Loguru. You should now instead include loguru.cpp (or build it and link with it)"
#error "You are defining LOGURU_IMPLEMENTATION. This is for older versions of Loguru. You should now instead include loguru.cpp (or build it and link with it)"
#endif

// Disable all warnings from gcc/clang:
Expand All @@ -93,7 +94,7 @@ Website: www.ilikebigbits.com

// Semantic versioning. Loguru version can be printed with printf("%d.%d.%d", LOGURU_VERSION_MAJOR, LOGURU_VERSION_MINOR, LOGURU_VERSION_PATCH);
#define LOGURU_VERSION_MAJOR 2
#define LOGURU_VERSION_MINOR 0
#define LOGURU_VERSION_MINOR 1
#define LOGURU_VERSION_PATCH 0

#if defined(_MSC_VER)
Expand Down Expand Up @@ -155,8 +156,8 @@ Website: www.ilikebigbits.com
#define LOGURU_WITH_STREAMS 1
#endif

#ifndef LOGURU_UNSAFE_SIGNAL_HANDLER
#define LOGURU_UNSAFE_SIGNAL_HANDLER 1
#if defined(LOGURU_UNSAFE_SIGNAL_HANDLER)
#error "You are defining LOGURU_UNSAFE_SIGNAL_HANDLER. This is for older versions of Loguru. You should now instead set the unsafe_signal_handler option when you call loguru::init."
#endif

#if LOGURU_IMPLEMENTATION
Expand Down Expand Up @@ -387,6 +388,27 @@ namespace loguru
// Verbosity_INVALID if name is not recognized.
typedef Verbosity (*name_to_verbosity_t)(const char* name);

// Runtime options passed to loguru::init
struct Options
{
// This allows you to use something else instead of "-v" via verbosity_flag.
// Set to nullptr to if you don't want Loguru to parse verbosity from the args.'
const char* verbosity_flag = "-v";

// loguru::init will set the name of the calling thread to this.
// If you don't want Loguru to set the name of the main thread,
// set this to nullptr.
// NOTE: on SOME platforms loguru::init will only overwrite the thread name
// if a thread name has not already been set.
// To always set a thread name, use loguru::set_thread_name instead.
const char* main_thread_name = "main thread";

// Make Loguru try to do unsafe but useful things,
// like printing a stack trace, when catching signals.
// This may lead to bad things like deadlocks in certain situations.
bool unsafe_signal_handler = true;
};

/* Should be called from the main thread.
You don't *need* to call this, but if you do you get:
* Signal handlers installed
Expand All @@ -411,11 +433,10 @@ namespace loguru
That way you can set the default but have the user override it with the -v flag.
Note that -v does not affect file logging (see loguru::add_file).
You can use something else instead of "-v" via verbosity_flag.
You can also set verbosity_flag to nullptr.
You can you something other than the -v flag by setting the verbosity_flag option.
*/
LOGURU_EXPORT
void init(int& argc, char* argv[], const char* verbosity_flag = "-v");
void init(int& argc, char* argv[], const Options& options = {});

// Will call remove_all_callbacks(). After calling this, logging will still go to stderr.
// You generally don't need to call this.
Expand Down

0 comments on commit 2ed007e

Please sign in to comment.