Skip to content

Commit

Permalink
common: drop the logging context
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Michalski <jan.michalski@intel.com>
  • Loading branch information
janekmi committed Mar 7, 2024
1 parent dde2f7e commit def6b52
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 74 deletions.
29 changes: 6 additions & 23 deletions src/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ _Atomic
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
uintptr_t Core_log_function = 0;

/* the logging function's context */
#ifdef ATOMIC_OPERATIONS_SUPPORTED
_Atomic
#endif /* ATOMIC_OPERATIONS_SUPPORTED */
void *Core_log_function_context;

/* threshold levels */
enum core_log_level Core_log_threshold[] = {
CORE_LOG_THRESHOLD_DEFAULT,
Expand All @@ -78,7 +72,7 @@ core_log_init()
/* enable the default logging function */
core_log_default_init();
while (EAGAIN ==
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION, NULL))
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION))
;
}

Expand All @@ -94,7 +88,6 @@ core_log_fini()
* logging function.
*/
Core_log_function = 0;
Core_log_function_context = NULL;

/* cleanup the default logging function */
core_log_default_fini();
Expand All @@ -117,7 +110,7 @@ core_log_lib_info(void)
* a user-provided function pointer or to the default logging function.
*/
int
core_log_set_function(core_log_function *log_function, void *context)
core_log_set_function(core_log_function *log_function)
{

if (log_function == CORE_LOG_USE_DEFAULT_FUNCTION)
Expand All @@ -126,25 +119,15 @@ core_log_set_function(core_log_function *log_function, void *context)
#ifdef ATOMIC_OPERATIONS_SUPPORTED
atomic_store_explicit(&Core_log_function, (uintptr_t)log_function,
__ATOMIC_SEQ_CST);
atomic_store_explicit(&Core_log_function_context, context,
__ATOMIC_SEQ_CST);
return 0;
#else
uintptr_t core_log_function_old = Core_log_function;
void *context_old = Core_log_function_context;
if (!__sync_bool_compare_and_swap(&Core_log_function,
core_log_function_old, (uintptr_t)log_function))
return EAGAIN;
if (__sync_bool_compare_and_swap(&Core_log_function_context,
context_old, context)) {
if (__sync_bool_compare_and_swap(&Core_log_function,
core_log_function_old, (uintptr_t)log_function)) {
core_log_lib_info();
return 0;
}

(void) __sync_bool_compare_and_swap(&Core_log_function,
(uintptr_t)log_function, core_log_function_old);
return EAGAIN;

#endif /* ATOMIC_OPERATIONS_SUPPORTED */
}

Expand Down Expand Up @@ -226,8 +209,8 @@ core_log_va(char *buf, size_t buf_len, enum core_log_level level,
if (0 == Core_log_function)
goto end;

((core_log_function *)Core_log_function)(Core_log_function_context,
level, file_name, line_no, function_name, buf);
((core_log_function *)Core_log_function)(level, file_name, line_no,
function_name, buf);

end:
if (errnum != NO_ERRNO)
Expand Down
7 changes: 2 additions & 5 deletions src/core/log_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ get_timestamp_prefix(char *buf, size_t buf_size)
* - file == NULL || (file != NULL && function != NULL)
*/
void
core_log_default_function(void *context, enum core_log_level level,
const char *file_name, const int line_no, const char *function_name,
const char *message)
core_log_default_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name, const char *message)
{
SUPPRESS_UNUSED(context);

char file_info_buffer[256] = "";
const char *file_info = file_info_buffer;
const char file_info_error[] = "[file info error]: ";
Expand Down
5 changes: 2 additions & 3 deletions src/core/log_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#ifndef CORE_LOG_DEFAULT_H
#define CORE_LOG_DEFAULT_H

void core_log_default_function(void *context, enum core_log_level level,
const char *file_name, const int line_no, const char *function_name,
const char *message);
void core_log_default_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name, const char *message);

void core_log_default_init(void);

Expand Down
4 changes: 1 addition & 3 deletions src/core/log_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ int core_log_get_threshold(enum core_log_threshold threshold,
* the type used for defining logging functions
*/
typedef void core_log_function(
/* the context provided when setting the log function */
void *context,
/* the log level of the message */
enum core_log_level level,
/* name of the source file where the message coming from */
Expand All @@ -81,7 +79,7 @@ typedef void core_log_function(

#define CORE_LOG_USE_DEFAULT_FUNCTION (NULL)

int core_log_set_function(core_log_function *log_function, void *context);
int core_log_set_function(core_log_function *log_function);

/* threshold levels */
extern
Expand Down
6 changes: 2 additions & 4 deletions src/core/out.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ static const int core_log_level_to_level[CORE_LOG_LEVEL_MAX] = {
#define OUT_MAX_LEVEL 4

static void
out_legacy(void *context, enum core_log_level core_level, const char *file_name,
out_legacy(enum core_log_level core_level, const char *file_name,
const int line_no, const char *function_name, const char *message)
{
SUPPRESS_UNUSED(context);

int level = core_log_level_to_level[core_level];
out_log(file_name, line_no, function_name, level, "%s", message);
}
Expand Down Expand Up @@ -149,7 +147,7 @@ out_init(const char *log_prefix, const char *log_level_var,
}

if (log_level != NULL || log_file != NULL) {
ret = core_log_set_function(out_legacy, NULL);
ret = core_log_set_function(out_legacy);
if (ret) {
CORE_LOG_FATAL("Cannot set legacy log function");
}
Expand Down
12 changes: 2 additions & 10 deletions src/include/libpmemobj/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ int pmemobj_log_get_threshold(enum pmemobj_log_threshold threshold,
* the type used for defining logging functions
*/
typedef void pmemobj_log_function(
/* the context provided when setting the log function */
void *context,
/* the log level of the message */
enum pmemobj_log_level level,
/* name of the source file where the message coming from */
Expand All @@ -176,26 +174,20 @@ typedef void pmemobj_log_function(
* SYNOPSIS
*
* typedef void pmemobj_log_function(
* void *context,
* enum pmemobj_log_level level,
* const char *file_name,
* const int line_no,
* const char *function_name,
* const char *message_format,
* ...);
*
* int pmemobj_log_set_function(pmemobj_log_function *log_function,
* void *context);
* int pmemobj_log_set_function(pmemobj_log_function *log_function);
*
* DESCRIPTION
* pmemobj_log_set_function() allows choosing the function which will get all
* the generated logging messages. The log_function can be either
* PMEMOBJ_LOG_USE_DEFAULT_FUNCTION which will use the default logging function
* (built into the library) or a pointer to a user-defined function.
* The context allows to pass an additional value which will be passed along
* with all the logging messages to the logging function. When the provided
* log_function is PMEMOBJ_LOG_USE_DEFAULT_FUNCTION the provided context is
* ignored.
*
* Parameters of a user-defined log function are as follow:
* - level - the log level of the message
Expand Down Expand Up @@ -228,7 +220,7 @@ typedef void pmemobj_log_function(
* SEE ALSO
* pmemobj_log_get_threshold(3), pmemobj_log_set_threshold(3).
*/
int pmemobj_log_set_function(pmemobj_log_function *log_function, void *context);
int pmemobj_log_set_function(pmemobj_log_function *log_function);

#ifdef __cplusplus
}
Expand Down
8 changes: 2 additions & 6 deletions src/libpmemobj/obj_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ pmemobj_log_get_threshold(enum pmemobj_log_threshold threshold,
* a user-provided function pointer or to the default logging function.
*/
int
pmemobj_log_set_function(pmemobj_log_function *log_function, void *context)
pmemobj_log_set_function(pmemobj_log_function *log_function)
{
if (log_function == PMEMOBJ_LOG_USE_DEFAULT_FUNCTION)
context = NULL;

int ret = core_log_set_function((core_log_function *)log_function,
context);
int ret = core_log_set_function((core_log_function *)log_function);
return core_log_error_translate(ret);
}
2 changes: 1 addition & 1 deletion src/test/core_log/core_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ int
main(int argc, char *argv[])
{
START(argc, argv, "core_log");
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION, NULL);
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION);
openlog(NULL, LOG_NDELAY, LOG_USER);
TEST_CASE_PROCESS(argc, argv, test_cases, NTESTS);
closelog();
Expand Down
11 changes: 5 additions & 6 deletions src/test/core_log/core_log_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ FUNC_MOCK_RUN_DEFAULT {
}
FUNC_MOCK_END

FUNC_MOCK(core_log_default_function, void, void *context,
enum core_log_level level, const char *file_name, const int line_no,
const char *function_name, const char *message)
FUNC_MOCK(core_log_default_function, void, enum core_log_level level,
const char *file_name, const int line_no, const char *function_name,
const char *message)
FUNC_MOCK_RUN(VALIDATED_CALL) {
UT_ASSERTeq(context, NULL);
UT_ASSERTeq(level, Log_function_.exp_level);
UT_ASSERTstreq(file_name, FILE_NAME);
UT_ASSERTeq(line_no, LINE_NO);
Expand All @@ -79,8 +78,8 @@ FUNC_MOCK(core_log_default_function, void, void *context,
return;
}
FUNC_MOCK_RUN_DEFAULT {
_FUNC_REAL(core_log_default_function)(context, level, file_name,
line_no, function_name, message);
_FUNC_REAL(core_log_default_function)(level, file_name, line_no,
function_name, message);
}
FUNC_MOCK_END

Expand Down
16 changes: 8 additions & 8 deletions src/test/core_log_default_function/core_log_default_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ test_default_function(const struct test_case *tc, int argc, char *argv[])
for (enum core_log_level level = CORE_LOG_LEVEL_HARK;
level < CORE_LOG_LEVEL_MAX; level++) {
TEST_STEP_SETUP(level, FILE_NAME);
core_log_default_function(NULL, level, FILE_NAME_W_PATH,
core_log_default_function(level, FILE_NAME_W_PATH,
LINE_NO, FUNCTION_NAME, MESSAGE_MOCK);
if (level == CORE_LOG_LEVEL_HARK || level > treshold)
TEST_STEP_CHECK(1, 0);
Expand All @@ -211,7 +211,7 @@ test_default_function_bad_file_name(const struct test_case *tc, int argc,
TEST_STEP_SETUP(CORE_LOG_LEVEL_DEBUG, FILE_INFO_ERROR);
Snprintf.ret = -1;
Common.exp_file_info = FILE_INFO_ERROR;
core_log_default_function(NULL, CORE_LOG_LEVEL_DEBUG, FILE_NAME_W_PATH,
core_log_default_function(CORE_LOG_LEVEL_DEBUG, FILE_NAME_W_PATH,
LINE_NO, FUNCTION_NAME, MESSAGE_MOCK);
TEST_STEP_CHECK(2, 1);

Expand All @@ -227,7 +227,7 @@ test_default_function_short_file_name(const struct test_case *tc, int argc,
TEST_SETUP();
TEST_STEP_SETUP(CORE_LOG_LEVEL_DEBUG, FILE_NAME);
Strchr_ret = NULL;
core_log_default_function(NULL, CORE_LOG_LEVEL_DEBUG, FILE_NAME,
core_log_default_function(CORE_LOG_LEVEL_DEBUG, FILE_NAME,
LINE_NO, FUNCTION_NAME, MESSAGE_MOCK);
TEST_STEP_CHECK(2, 1);

Expand All @@ -243,7 +243,7 @@ test_default_function_no_file_name(const struct test_case *tc, int argc,
TEST_STEP_SETUP(CORE_LOG_LEVEL_DEBUG, "");
FUNC_MOCK_RCOUNTER_SET(snprintf, 1); /* skip file_info snprintf() */
Common.exp_file_info = "";
core_log_default_function(NULL, CORE_LOG_LEVEL_DEBUG, NULL,
core_log_default_function(CORE_LOG_LEVEL_DEBUG, NULL,
LINE_NO, FUNCTION_NAME, MESSAGE_MOCK);
TEST_STEP_CHECK(2, 1);

Expand All @@ -259,8 +259,8 @@ test_default_function_no_function_name(const struct test_case *tc, int argc,
TEST_STEP_SETUP(CORE_LOG_LEVEL_DEBUG, "");
FUNC_MOCK_RCOUNTER_SET(snprintf, 1); /* skip file_info snprintf() */
Common.exp_file_info = "";
core_log_default_function(NULL, CORE_LOG_LEVEL_DEBUG, NULL,
LINE_NO, NULL, MESSAGE_MOCK);
core_log_default_function(CORE_LOG_LEVEL_DEBUG, NULL, LINE_NO, NULL,
MESSAGE_MOCK);
TEST_STEP_CHECK(2, 1);

return NO_ARGS_CONSUMED;
Expand All @@ -275,8 +275,8 @@ test_default_function_bad_timestamp(const struct test_case *tc, int argc,
TEST_STEP_SETUP(CORE_LOG_LEVEL_DEBUG, FILE_NAME);
Os_clock_gettime_force_error = true; /* fail the file_info snprintf() */
Fprintf.exp_times_stamp = "[time error] ";
core_log_default_function(NULL, CORE_LOG_LEVEL_DEBUG, FILE_NAME,
LINE_NO, FUNCTION_NAME, MESSAGE_MOCK);
core_log_default_function(CORE_LOG_LEVEL_DEBUG, FILE_NAME, LINE_NO,
FUNCTION_NAME, MESSAGE_MOCK);
TEST_STEP_CHECK(1, 1);

return NO_ARGS_CONSUMED;
Expand Down
2 changes: 1 addition & 1 deletion src/test/core_log_internal/core_log_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ int
main(int argc, char *argv[])
{
core_log_get_threshold(CORE_LOG_THRESHOLD, &Core_log_default_threshold);
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION, NULL);
core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION);

START(argc, argv, "core_log_internal");
TEST_CASE_PROCESS(argc, argv, test_cases, NTESTS);
Expand Down
6 changes: 3 additions & 3 deletions src/test/unittest/unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ void ut_err(const char *file, int line, const char *func,
__attribute__((format(printf, 4, 5)));

void
ut_log_function(void *context, enum core_log_level level, const char *file_name,
ut_log_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name,
const char *message);

#ifdef USE_LOG_PMEMCORE
#define LOG_SET_PMEMCORE_FUNC core_log_set_function(ut_log_function, NULL)
#define LOG_SET_PMEMCORE_FUNC core_log_set_function(ut_log_function)
#else
#define LOG_SET_PMEMCORE_FUNC
#endif

#ifdef USE_LOG_PMEMOBJ
#define LOG_SET_PMEMOBJ_FUNC \
pmemobj_log_set_function((pmemobj_log_function *)ut_log_function, NULL)
pmemobj_log_set_function((pmemobj_log_function *)ut_log_function)
#else
#define LOG_SET_PMEMOBJ_FUNC
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/test/unittest/ut_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static const int core_log_level_to_out_level[] = {
};

void
ut_log_function(void *context, enum core_log_level level, const char *file_name,
ut_log_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name, const char *message)
{
out_log(file_name, line_no, function_name,
Expand Down

0 comments on commit def6b52

Please sign in to comment.