log: Always enable; remove DISABLE_LOG

- Establish ERROR log level as "critical". Such errors are rare and will
  be valuable when users encounter unusual circumstances.
- Set -DMIN_LOG_LEVEL=3 for release-type builds
This commit is contained in:
Justin M. Keyes 2017-05-30 02:03:08 +02:00
parent 698ec9eb6e
commit fe1af9c2bc
11 changed files with 65 additions and 73 deletions

View File

@ -27,8 +27,7 @@ env:
-DCMAKE_INSTALL_PREFIX:PATH=$INSTALL_PREFIX -DCMAKE_INSTALL_PREFIX:PATH=$INSTALL_PREFIX
-DBUSTED_OUTPUT_TYPE=nvim -DBUSTED_OUTPUT_TYPE=nvim
-DDEPS_PREFIX=$DEPS_BUILD_DIR/usr -DDEPS_PREFIX=$DEPS_BUILD_DIR/usr
-DMIN_LOG_LEVEL=3 -DMIN_LOG_LEVEL=3"
-DDISABLE_LOG=1"
- DEPS_CMAKE_FLAGS="-DDEPS_DOWNLOAD_DIR:PATH=$DEPS_DOWNLOAD_DIR" - DEPS_CMAKE_FLAGS="-DDEPS_DOWNLOAD_DIR:PATH=$DEPS_DOWNLOAD_DIR"
# Additional CMake flags for 32-bit builds. # Additional CMake flags for 32-bit builds.
- CMAKE_FLAGS_32BIT="-DCMAKE_SYSTEM_LIBRARY_PATH=/lib32:/usr/lib32:/usr/local/lib32 - CMAKE_FLAGS_32BIT="-DCMAKE_SYSTEM_LIBRARY_PATH=/lib32:/usr/lib32:/usr/local/lib32

View File

@ -96,15 +96,15 @@ if(CMAKE_C_FLAGS_RELEASE MATCHES "-O3")
string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}") string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
endif() endif()
# Disable logging for release-type builds. # Minimize logging for release-type builds.
if(NOT CMAKE_C_FLAGS_RELEASE MATCHES DDISABLE_LOG) if(NOT CMAKE_C_FLAGS_RELEASE MATCHES DMIN_LOG_LEVEL)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DDISABLE_LOG") set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DMIN_LOG_LEVEL=3")
endif() endif()
if(NOT CMAKE_C_FLAGS_MINSIZEREL MATCHES DDISABLE_LOG) if(NOT CMAKE_C_FLAGS_MINSIZEREL MATCHES DMIN_LOG_LEVEL)
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -DDISABLE_LOG") set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -DMIN_LOG_LEVEL=3")
endif() endif()
if(NOT CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DDISABLE_LOG) if(NOT CMAKE_C_FLAGS_RELWITHDEBINFO MATCHES DMIN_LOG_LEVEL)
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DDISABLE_LOG") set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -DMIN_LOG_LEVEL=3")
endif() endif()
# Enable assertions for RelWithDebInfo. # Enable assertions for RelWithDebInfo.
@ -463,11 +463,11 @@ install_helper(
# MIN_LOG_LEVEL for log.h # MIN_LOG_LEVEL for log.h
if(DEFINED MIN_LOG_LEVEL) if(DEFINED MIN_LOG_LEVEL)
if(NOT MIN_LOG_LEVEL MATCHES "^[0-3]$") if(NOT MIN_LOG_LEVEL MATCHES "^[0-3]$")
message(FATAL_ERROR "MIN_LOG_LEVEL must be a number 0-3") message(FATAL_ERROR "invalid MIN_LOG_LEVEL: " ${MIN_LOG_LEVEL})
endif() endif()
message(STATUS "MIN_LOG_LEVEL set to ${MIN_LOG_LEVEL}") message(STATUS "MIN_LOG_LEVEL set to ${MIN_LOG_LEVEL}")
else() else()
message(STATUS "MIN_LOG_LEVEL not specified, defaulting to INFO(1)") message(STATUS "MIN_LOG_LEVEL not specified, defaulting to 1 (INFO)")
endif() endif()
# Go down the tree. # Go down the tree.

View File

@ -31,9 +31,8 @@
# #
# CMAKE_BUILD_TYPE := Debug # CMAKE_BUILD_TYPE := Debug
# By default, nvim's log level is INFO (1) (unless CMAKE_BUILD_TYPE is # The default log level is 1 (INFO) (unless CMAKE_BUILD_TYPE is "Release").
# "Release", in which case logging is disabled). # Log levels: 0 (DEBUG), 1 (INFO), 2 (WARNING), 3 (ERROR)
# The log level must be a number DEBUG (0), INFO (1), WARNING (2) or ERROR (3).
# CMAKE_EXTRA_FLAGS += -DMIN_LOG_LEVEL=1 # CMAKE_EXTRA_FLAGS += -DMIN_LOG_LEVEL=1
# By default, nvim uses bundled versions of its required third-party # By default, nvim uses bundled versions of its required third-party

View File

@ -260,7 +260,7 @@ int cursor_mode_str2int(const char *mode)
return current_mode; return current_mode;
} }
} }
ELOG("Unknown mode %s", mode); WLOG("Unknown mode %s", mode);
return -1; return -1;
} }

View File

@ -14361,7 +14361,7 @@ static void f_serverstart(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (result != 0) { if (result != 0) {
EMSG2("Failed to start server: %s", EMSG2("Failed to start server: %s",
result > 0 ? "Unknonwn system error" : uv_strerror(result)); result > 0 ? "Unknown system error" : uv_strerror(result));
return; return;
} }

View File

@ -37,7 +37,7 @@ typedef struct growarray {
static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size) static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size)
{ {
if ((int)item_size != gap->ga_itemsize) { if ((int)item_size != gap->ga_itemsize) {
ELOG("wrong item size in garray(%d), should be %d", item_size); WLOG("wrong item size in garray(%d), should be %d", item_size);
} }
ga_grow(gap, 1); ga_grow(gap, 1);
return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++); return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++);

View File

@ -76,6 +76,10 @@ void log_unlock(void)
bool do_log(int log_level, const char *func_name, int line_num, bool eol, bool do_log(int log_level, const char *func_name, int line_num, bool eol,
const char* fmt, ...) FUNC_ATTR_UNUSED const char* fmt, ...) FUNC_ATTR_UNUSED
{ {
if (log_level < MIN_LOG_LEVEL) {
return false;
}
log_lock(); log_lock();
bool ret = false; bool ret = false;
FILE *log_file = open_log_file(); FILE *log_file = open_log_file();
@ -124,11 +128,10 @@ end:
FILE *open_log_file(void) FILE *open_log_file(void)
{ {
static bool opening_log_file = false; static bool opening_log_file = false;
// check if it's a recursive call // check if it's a recursive call
if (opening_log_file) { if (opening_log_file) {
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true, do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
"Trying to LOG() recursively! Please fix it."); "Cannot LOG() recursively.");
return stderr; return stderr;
} }
@ -145,9 +148,8 @@ FILE *open_log_file(void)
} }
do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true, do_log_to_file(stderr, ERROR_LOG_LEVEL, __func__, __LINE__, true,
"Couldn't open USR_LOG_FILE, logging to stderr! This may be " "Cannot open " USR_LOG_FILE ", logging to stderr. Was LOG()"
"caused by attempting to LOG() before initialization " " called before early_init()?");
"functions are called (e.g. init_homedir()).");
return stderr; return stderr;
} }
@ -172,7 +174,7 @@ static bool v_do_log_to_file(FILE *log_file, int log_level,
[DEBUG_LOG_LEVEL] = "DEBUG", [DEBUG_LOG_LEVEL] = "DEBUG",
[INFO_LOG_LEVEL] = "INFO ", [INFO_LOG_LEVEL] = "INFO ",
[WARNING_LOG_LEVEL] = "WARN ", [WARNING_LOG_LEVEL] = "WARN ",
[ERROR_LOG_LEVEL] = "ERROR" [ERROR_LOG_LEVEL] = "ERROR",
}; };
assert(log_level >= DEBUG_LOG_LEVEL && log_level <= ERROR_LOG_LEVEL); assert(log_level >= DEBUG_LOG_LEVEL && log_level <= ERROR_LOG_LEVEL);

View File

@ -18,55 +18,47 @@
#define ELOG(...) #define ELOG(...)
#define ELOGN(...) #define ELOGN(...)
// Logging is disabled if NDEBUG or DISABLE_LOG is defined.
#if !defined(DISABLE_LOG) && defined(NDEBUG)
# define DISABLE_LOG
#endif
// MIN_LOG_LEVEL can be defined during compilation to adjust the desired level
// of logging. INFO_LOG_LEVEL is used by default.
#ifndef MIN_LOG_LEVEL #ifndef MIN_LOG_LEVEL
# define MIN_LOG_LEVEL INFO_LOG_LEVEL # define MIN_LOG_LEVEL INFO_LOG_LEVEL
#endif #endif
#ifndef DISABLE_LOG #define LOG(level, ...) do_log((level), __func__, __LINE__, true, \
__VA_ARGS__)
# if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL #if MIN_LOG_LEVEL <= DEBUG_LOG_LEVEL
# undef DLOG # undef DLOG
# undef DLOGN # undef DLOGN
# define DLOG(...) do_log(DEBUG_LOG_LEVEL, __func__, __LINE__, true, \ # define DLOG(...) do_log(DEBUG_LOG_LEVEL, __func__, __LINE__, true, \
__VA_ARGS__) __VA_ARGS__)
# define DLOGN(...) do_log(DEBUG_LOG_LEVEL, __func__, __LINE__, false, \ # define DLOGN(...) do_log(DEBUG_LOG_LEVEL, __func__, __LINE__, false, \
__VA_ARGS__) __VA_ARGS__)
# endif #endif
# if MIN_LOG_LEVEL <= INFO_LOG_LEVEL #if MIN_LOG_LEVEL <= INFO_LOG_LEVEL
# undef ILOG # undef ILOG
# undef ILOGN # undef ILOGN
# define ILOG(...) do_log(INFO_LOG_LEVEL, __func__, __LINE__, true, \ # define ILOG(...) do_log(INFO_LOG_LEVEL, __func__, __LINE__, true, \
__VA_ARGS__) __VA_ARGS__)
# define ILOGN(...) do_log(INFO_LOG_LEVEL, __func__, __LINE__, false, \ # define ILOGN(...) do_log(INFO_LOG_LEVEL, __func__, __LINE__, false, \
__VA_ARGS__) __VA_ARGS__)
# endif #endif
# if MIN_LOG_LEVEL <= WARNING_LOG_LEVEL #if MIN_LOG_LEVEL <= WARNING_LOG_LEVEL
# undef WLOG # undef WLOG
# undef WLOGN # undef WLOGN
# define WLOG(...) do_log(WARNING_LOG_LEVEL, __func__, __LINE__, true, \ # define WLOG(...) do_log(WARNING_LOG_LEVEL, __func__, __LINE__, true, \
__VA_ARGS__) __VA_ARGS__)
# define WLOGN(...) do_log(WARNING_LOG_LEVEL, __func__, __LINE__, false, \ # define WLOGN(...) do_log(WARNING_LOG_LEVEL, __func__, __LINE__, false, \
__VA_ARGS__) __VA_ARGS__)
# endif #endif
# if MIN_LOG_LEVEL <= ERROR_LOG_LEVEL
# undef ELOG
# undef ELOGN
# define ELOG(...) do_log(ERROR_LOG_LEVEL, __func__, __LINE__, true, \
__VA_ARGS__)
# define ELOGN(...) do_log(ERROR_LOG_LEVEL, __func__, __LINE__, false, \
__VA_ARGS__)
# endif
#if MIN_LOG_LEVEL <= ERROR_LOG_LEVEL
# undef ELOG
# undef ELOGN
# define ELOG(...) do_log(ERROR_LOG_LEVEL, __func__, __LINE__, true, \
__VA_ARGS__)
# define ELOGN(...) do_log(ERROR_LOG_LEVEL, __func__, __LINE__, false, \
__VA_ARGS__)
#endif #endif
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS

View File

@ -370,7 +370,7 @@ static void receive_msgpack(Stream *stream, RBuffer *rbuf, size_t c,
char buf[256]; char buf[256];
snprintf(buf, sizeof(buf), "ch %" PRIu64 " was closed by the client", snprintf(buf, sizeof(buf), "ch %" PRIu64 " was closed by the client",
channel->id); channel->id);
call_set_error(channel, buf); call_set_error(channel, buf, WARNING_LOG_LEVEL);
goto end; goto end;
} }
@ -409,7 +409,7 @@ static void parse_msgpack(Channel *channel)
"ch %" PRIu64 " returned a response with an unknown request " "ch %" PRIu64 " returned a response with an unknown request "
"id. Ensure the client is properly synchronized", "id. Ensure the client is properly synchronized",
channel->id); channel->id);
call_set_error(channel, buf); call_set_error(channel, buf, ERROR_LOG_LEVEL);
} }
msgpack_unpacked_destroy(&unpacked); msgpack_unpacked_destroy(&unpacked);
// Bail out from this event loop iteration // Bail out from this event loop iteration
@ -459,7 +459,7 @@ static void handle_request(Channel *channel, msgpack_object *request)
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
"ch %" PRIu64 " sent an invalid message, closed.", "ch %" PRIu64 " sent an invalid message, closed.",
channel->id); channel->id);
call_set_error(channel, buf); call_set_error(channel, buf, ERROR_LOG_LEVEL);
} }
api_clear_error(&error); api_clear_error(&error);
return; return;
@ -564,7 +564,7 @@ static bool channel_write(Channel *channel, WBuffer *buffer)
"Before returning from a RPC call, ch %" PRIu64 " was " "Before returning from a RPC call, ch %" PRIu64 " was "
"closed due to a failed write", "closed due to a failed write",
channel->id); channel->id);
call_set_error(channel, buf); call_set_error(channel, buf, ERROR_LOG_LEVEL);
} }
return success; return success;
@ -795,9 +795,9 @@ static void complete_call(msgpack_object *obj, Channel *channel)
} }
} }
static void call_set_error(Channel *channel, char *msg) static void call_set_error(Channel *channel, char *msg, int loglevel)
{ {
ELOG("RPC: %s", msg); LOG(loglevel, "RPC: %s", msg);
for (size_t i = 0; i < kv_size(channel->call_stack); i++) { for (size_t i = 0; i < kv_size(channel->call_stack); i++) {
ChannelCallFrame *frame = kv_A(channel->call_stack, i); ChannelCallFrame *frame = kv_A(channel->call_stack, i);
frame->returned = true; frame->returned = true;

View File

@ -125,7 +125,7 @@ bool server_owns_pipe_address(const char *path)
int server_start(const char *endpoint) int server_start(const char *endpoint)
{ {
if (endpoint == NULL || endpoint[0] == '\0') { if (endpoint == NULL || endpoint[0] == '\0') {
ELOG("Empty or NULL endpoint"); WLOG("Empty or NULL endpoint");
return 1; return 1;
} }
@ -151,7 +151,7 @@ int server_start(const char *endpoint)
result = socket_watcher_start(watcher, MAX_CONNECTIONS, connection_cb); result = socket_watcher_start(watcher, MAX_CONNECTIONS, connection_cb);
if (result < 0) { if (result < 0) {
ELOG("Failed to start server: %s", uv_strerror(result)); WLOG("Failed to start server: %s", uv_strerror(result));
socket_watcher_close(watcher, free_server); socket_watcher_close(watcher, free_server);
return result; return result;
} }

View File

@ -685,7 +685,7 @@ static void shell_write_cb(Stream *stream, void *data, int status)
uv_err_name(status)); uv_err_name(status));
} }
if (stream->closed) { // Process may have exited before this write. if (stream->closed) { // Process may have exited before this write.
ELOG("stream was already closed"); WLOG("stream was already closed");
return; return;
} }
stream_close(stream, NULL, NULL); stream_close(stream, NULL, NULL);