Skip to content

Commit

Permalink
refactor: split dlt_common and dlt_log (#658)
Browse files Browse the repository at this point in the history
Seperate all internal logging function into an own source file.
This reduces the scope of the sprawling dlt_common source file.

Co-authored-by: Daniel Weber <[email protected]>
  • Loading branch information
duvanan13 and danielweber2018 authored Jul 5, 2024
1 parent e1b22b5 commit 358ab08
Show file tree
Hide file tree
Showing 23 changed files with 529 additions and 412 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ cc_binary {
"src/gateway/dlt_gateway.c",
"src/lib/dlt_client.c",
"src/shared/dlt_common.c",
"src/shared/dlt_log.c",
"src/shared/dlt_config_file_parser.c",
"src/shared/dlt_multiple_files.c",
"src/shared/dlt_offline_trace.c",
Expand Down Expand Up @@ -142,6 +143,7 @@ cc_library_shared {
"src/lib/dlt_user.c",
"src/shared/dlt_common.c",
"src/shared/dlt_multiple_files.c",
"src/shared/dlt_log.c",
"src/shared/dlt_protocol.c",
"src/shared/dlt_user_shared.c",
],
Expand Down
2 changes: 1 addition & 1 deletion include/dlt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ endif()
configure_file(dlt_user.h.in dlt_user.h)

set(HEADER_LIST dlt.h dlt_user_macros.h dlt_client.h dlt_protocol.h
dlt_common.h dlt_types.h dlt_shm.h dlt_offline_trace.h
dlt_common.h dlt_log.h dlt_types.h dlt_shm.h dlt_offline_trace.h
dlt_filetransfer.h dlt_common_api.h dlt_multiple_files.h
${CMAKE_CURRENT_BINARY_DIR}/dlt_version.h
${CMAKE_CURRENT_BINARY_DIR}/dlt_user.h)
Expand Down
91 changes: 1 addition & 90 deletions include/dlt/dlt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@

# include "dlt_types.h"
# include "dlt_protocol.h"
# include "dlt_log.h"

# define DLT_PACKED __attribute__((aligned(1), packed))

Expand Down Expand Up @@ -191,13 +192,6 @@
# define LOG_DAEMON (3 << 3)
# endif

typedef enum {
DLT_LOG_TO_CONSOLE = 0,
DLT_LOG_TO_SYSLOG = 1,
DLT_LOG_TO_FILE = 2,
DLT_LOG_TO_STDERR = 3,
DLT_LOG_DROPPED = 4
} DltLoggingMode;

/**
* The standard TCP Port used for DLT daemon, can be overwritten via -p \<port\> when starting dlt-daemon
Expand Down Expand Up @@ -1167,68 +1161,20 @@ DltReturnValue dlt_file_message(DltFile *file, int index, int verbose);
*/
DltReturnValue dlt_file_free(DltFile *file, int verbose);

/**
* Set internal logging filename if mode 2
* @param filename the filename
*/
void dlt_log_set_filename(const char *filename);
#if defined DLT_DAEMON_USE_FIFO_IPC || defined DLT_LIB_USE_FIFO_IPC
/**
* Set FIFO base direction
* @param pipe_dir the pipe direction
*/
void dlt_log_set_fifo_basedir(const char *pipe_dir);
#endif
/**
* Set internal logging level
* @param level the level
*/
void dlt_log_set_level(int level);

/**
* Set whether to print "name" and "unit" attributes in console output
* @param state true = with attributes, false = without attributes
*/
void dlt_print_with_attributes(bool state);

/**
* Initialize (external) logging facility
* @param mode positive, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
*/
DltReturnValue dlt_log_init(int mode);
/**
* Print with variable arguments to specified file descriptor by DLT_LOG_MODE environment variable (like fprintf)
* @param format format string for message
* @return negative value if there was an error or the total number of characters written is returned on success
*/
int dlt_user_printf(const char *format, ...) PRINTF_FORMAT(1, 2);
/**
* Log ASCII string with null-termination to (external) logging facility
* @param prio priority (see syslog() call)
* @param s Pointer to ASCII string with null-termination
* @return negative value if there was an error
*/
DltReturnValue dlt_log(int prio, char *s);
/**
* Log with variable arguments to (external) logging facility (like printf)
* @param prio priority (see syslog() call)
* @param format format string for log message
* @return negative value if there was an error
*/
DltReturnValue dlt_vlog(int prio, const char *format, ...) PRINTF_FORMAT(2, 3);
/**
* Log size bytes with variable arguments to (external) logging facility (similar to snprintf)
* @param prio priority (see syslog() call)
* @param size number of bytes to log
* @param format format string for log message
* @return negative value if there was an error
*/
DltReturnValue dlt_vnlog(int prio, size_t size, const char *format, ...) PRINTF_FORMAT(3, 4);
/**
* De-Initialize (external) logging facility
*/
void dlt_log_free(void);

/**
* Initialising a dlt receiver structure
* @param receiver pointer to dlt receiver structure
Expand Down Expand Up @@ -1686,41 +1632,6 @@ char *get_filename_ext(const char *filename);
*/
bool dlt_extract_base_name_without_ext(const char* const abs_file_name, char* base_name, long base_name_len);

/**
* Initialize (external) logging facility
* @param mode DltLoggingMode, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
* @param enable_multiple_logfiles, true if multiple logfiles (incl. size limits) should be use
* @param logging_file_size, maximum size in bytes of one logging file
* @param logging_files_max_size, maximum size in bytes of all logging files
*/
DltReturnValue dlt_log_init_multiple_logfiles_support(DltLoggingMode mode, bool enable_multiple_logfiles, int logging_file_size, int logging_files_max_size);

/**
* Initialize (external) logging facility for single logfile.
*/
DltReturnValue dlt_log_init_single_logfile();

/**
* Initialize (external) logging facility for multiple files logging.
*/
DltReturnValue dlt_log_init_multiple_logfiles(int logging_file_size, int logging_files_max_size);

/**
* Logs into log files represented by the multiple files buffer.
* @param format First element in a specific format that will be logged.
* @param ... Further elements in a specific format that will be logged.
*/
void dlt_log_multiple_files_write(const char* format, ...);

void dlt_log_free_single_logfile();

void dlt_log_free_multiple_logfiles();

/**
* Checks whether (internal) logging in multiple files is active.
*/
bool dlt_is_log_in_multiple_files_active();

# ifdef __cplusplus
}
# endif
Expand Down
152 changes: 152 additions & 0 deletions include/dlt/dlt_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* SPDX license identifier: MPL-2.0
*
* Copyright (C) 2024, Mercedes Benz Tech Innovation GmbH
*
* This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see https://www.covesa.global/.
*/

/*!
* \author
* Daniel Weber <[email protected]>
*
* \copyright Copyright © 2024 Mercedes Benz Tech Innovation GmbH. \n
* License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
*
* \file dlt_log.h
*/

#ifndef DLT_COMMON_LOG_H
#define DLT_COMMON_LOG_H

#include <stdio.h>
#include <stdbool.h>
#include "dlt_types.h"

# if defined(__GNUC__)
# define PURE_FUNCTION __attribute__((pure))
# define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, a, b)))
# else
# define PURE_FUNCTION /* nothing */
# define PRINTF_FORMAT(a,b) /* nothing */
# endif

typedef enum {
DLT_LOG_TO_CONSOLE = 0,
DLT_LOG_TO_SYSLOG = 1,
DLT_LOG_TO_FILE = 2,
DLT_LOG_TO_STDERR = 3,
DLT_LOG_DROPPED = 4
} DltLoggingMode;

/* initialize this variables in dlt_log.c */
extern DltLoggingMode logging_mode;
extern FILE *logging_handle;

# ifdef __cplusplus
extern "C"
{
# endif

/**
* Set internal logging filename if mode 2
* @param filename the filename
*/
void dlt_log_set_filename(const char *filename);

/**
* Set internal logging level
* @param level the level
*/
void dlt_log_set_level(int level);

/**
* Initialize (external) logging facility
* @param mode positive, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
*/
void dlt_log_init(int mode);

/**
* Initialize (external) logging facility
* @param mode DltLoggingMode, 0 = log to stdout, 1 = log to syslog, 2 = log to file, 3 = log to stderr
* @param enable_multiple_logfiles, true if multiple logfiles (incl. size limits) should be use
* @param logging_file_size, maximum size in bytes of one logging file
* @param logging_files_max_size, maximum size in bytes of all logging files
*/
DltReturnValue dlt_log_init_multiple_logfiles_support(DltLoggingMode mode, bool enable_multiple_logfiles, int logging_file_size, int logging_files_max_size);

/**
* Initialize (external) logging facility for single logfile.
*/
DltReturnValue dlt_log_init_single_logfile();

/**
* Initialize (external) logging facility for multiple files logging.
*/
DltReturnValue dlt_log_init_multiple_logfiles(int logging_file_size, int logging_files_max_size);

/**
* Print with variable arguments to specified file descriptor by DLT_LOG_MODE environment variable (like fprintf)
* @param format format string for message
* @return negative value if there was an error or the total number of characters written is returned on success
*/
int dlt_user_printf(const char *format, ...) PRINTF_FORMAT(1, 2);

/**
* Log ASCII string with null-termination to (external) logging facility
* @param prio priority (see syslog() call)
* @param s Pointer to ASCII string with null-termination
* @return negative value if there was an error
*/
DltReturnValue dlt_log(int prio, const char *s);

/**
* Log with variable arguments to (external) logging facility (like printf)
* @param prio priority (see syslog() call)
* @param format format string for log message
* @return negative value if there was an error
*/
DltReturnValue dlt_vlog(int prio, const char *format, ...) PRINTF_FORMAT(2, 3);

/**
* Log size bytes with variable arguments to (external) logging facility (similar to snprintf)
* @param prio priority (see syslog() call)
* @param size number of bytes to log
* @param format format string for log message
* @return negative value if there was an error
*/
DltReturnValue dlt_vnlog(int prio, size_t size, const char *format, ...) PRINTF_FORMAT(3, 4);

/**
* Logs into log files represented by the multiple files buffer.
* @param format First element in a specific format that will be logged.
* @param ... Further elements in a specific format that will be logged.
*/
void dlt_log_multiple_files_write(const char* format, ...);

/**
* De-Initialize (external) logging facility
*/
void dlt_log_free(void);

void dlt_log_free_single_logfile();

void dlt_log_free_multiple_logfiles();

/**
* Checks whether (internal) logging in multiple files is active.
*/
bool dlt_is_log_in_multiple_files_active();

# ifdef __cplusplus
}
# endif

#endif /* DLT_COMMON_LOG_H */
1 change: 1 addition & 0 deletions src/console/dlt-receive.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
# include <limits.h>
#endif
#include <inttypes.h>
#include "dlt_log.h"
#include "dlt_client.h"
#include "dlt-control-common.h"

Expand Down
1 change: 1 addition & 0 deletions src/daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(dlt_daemon_SRCS
${PROJECT_SOURCE_DIR}/src/lib/dlt_client.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_common.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_config_file_parser.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_log.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_multiple_files.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_offline_trace.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_protocol.c
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#endif

#include "dlt_types.h"
#include "dlt_log.h"
#include "dlt-daemon.h"
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_common_cfg.h"
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include <sys/socket.h> /* send() */

#include "dlt_types.h"
#include "dlt_log.h"
#include "dlt_daemon_common.h"
#include "dlt_daemon_common_cfg.h"
#include "dlt_user_shared.h"
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
# include <semaphore.h>
# include <stdbool.h>
# include "dlt_common.h"
# include "dlt_log.h"
# include "dlt_user.h"
# include "dlt_offline_logstorage.h"
# include "dlt_gateway_types.h"
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_common.h"
#include "dlt_common.h"
#include "dlt_log.h"
#include "dlt_gateway.h"
#include "dlt_daemon_socket.h"

Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_event_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <syslog.h>

#include "dlt_common.h"
#include "dlt_log.h"

#include "dlt-daemon.h"
#include "dlt-daemon_cfg.h"
Expand Down
1 change: 1 addition & 0 deletions src/daemon/dlt_daemon_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#endif

#include "dlt_types.h"
#include "dlt_log.h"
#include "dlt-daemon.h"
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_common_cfg.h"
Expand Down
1 change: 1 addition & 0 deletions src/gateway/dlt_gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "dlt_gateway_internal.h"
#include "dlt_config_file_parser.h"
#include "dlt_common.h"
#include "dlt_log.h"
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_common_cfg.h"
#include "dlt_daemon_event_handler.h"
Expand Down
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(dlt_LIB_SRCS
dlt_filetransfer.c
dlt_env_ll.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_common.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_log.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_multiple_files.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_protocol.c
${PROJECT_SOURCE_DIR}/src/shared/dlt_user_shared.c
Expand Down
1 change: 1 addition & 0 deletions src/lib/dlt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
#include <poll.h>

#include "dlt_types.h"
#include "dlt_log.h"
#include "dlt_client.h"
#include "dlt_client_cfg.h"

Expand Down
Loading

0 comments on commit 358ab08

Please sign in to comment.