summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLUONG HONG DUY KHANH(RBVH/ENG42) <KHANH.LUONGHONGDUY@vn.bosch.com>2020-03-18 13:40:43 +0700
committerSaya Sugiura <39760799+ssugiura@users.noreply.github.com>2020-07-06 10:04:07 +0900
commit156bb01a731661e0b90be90a58e56102352c33ad (patch)
treed87c1abe17dfff671477edf8f994b2f8b94e0f5d
parent573c1f9a0ba41b7ffa45f36e05b7f50aae3fb1ec (diff)
downloadDLT-daemon-156bb01a731661e0b90be90a58e56102352c33ad.tar.gz
Redirect stdout to stderr
- daemon: New feature for LoggingMode at /etc/dlt.conf to be able to set value at 3 in order to redirect all stdout messages to stderr. - libdlt: New feature for DLT_LOG_MODE environment variable to be able to set value at 3 in order to redirect all stdout messages to stderr. - unit test: Add test cases for DLT_LOG_TO_STDERR Signed-off-by: LUONG HONG DUY KHANH(RBVH/ENG42) <KHANH.LUONGHONGDUY@vn.bosch.com>
-rw-r--r--doc/dlt.conf.5.md2
-rw-r--r--include/dlt/dlt_common.h11
-rw-r--r--src/daemon/dlt.conf5
-rw-r--r--src/shared/dlt_common.c58
-rw-r--r--tests/gtest_dlt_common.cpp14
5 files changed, 69 insertions, 21 deletions
diff --git a/doc/dlt.conf.5.md b/doc/dlt.conf.5.md
index 6dd9ee5..4b30150 100644
--- a/doc/dlt.conf.5.md
+++ b/doc/dlt.conf.5.md
@@ -63,7 +63,7 @@ This is the directory path, where the DLT daemon stores its runtime configuratio
## LoggingMode
-The logging console for internal logging of dlt-daemon. 0 = log to stdout, 1 = log to syslog, 2 = log to file (see LoggingFilename)
+The logging console for internal logging of dlt-daemon. 0 = log to stdout, 1 = log to syslog, 2 = log to file (see LoggingFilename), 3 = log to stderr
Default: 0
diff --git a/include/dlt/dlt_common.h b/include/dlt/dlt_common.h
index e5e4a22..026cf75 100644
--- a/include/dlt/dlt_common.h
+++ b/include/dlt/dlt_common.h
@@ -185,7 +185,8 @@ enum {
DLT_LOG_TO_CONSOLE = 0,
DLT_LOG_TO_SYSLOG = 1,
DLT_LOG_TO_FILE = 2,
- DLT_LOG_DROPPED = 3
+ DLT_LOG_TO_STDERR = 3,
+ DLT_LOG_DROPPED = 4
};
/**
@@ -1127,10 +1128,16 @@ void dlt_log_set_filename(const char *filename);
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
+ * @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);
/**
+ * 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, ...);
+/**
* 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
diff --git a/src/daemon/dlt.conf b/src/daemon/dlt.conf
index 091f0d6..60d3315 100644
--- a/src/daemon/dlt.conf
+++ b/src/daemon/dlt.conf
@@ -35,7 +35,10 @@ SharedMemorySize = 100000
# PersistanceStoragePath = /tmp
# The logging console for internal logging of dlt-daemon (Default: 0)
-# 0 = log to stdout, 1 = log to syslog, 2 = log to file (see LoggingFilename)
+# 0 = log to stdout
+# 1 = log to syslog
+# 2 = log to file (see LoggingFilename)
+# 3 = log to stderr
LoggingMode = 0
# The internal log level, up to which logs are written (Default: 6)
diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c
index 670b01b..c462ed0 100644
--- a/src/shared/dlt_common.c
+++ b/src/shared/dlt_common.c
@@ -110,9 +110,9 @@ void dlt_print_hex(uint8_t *ptr, int size)
for (num = 0; num < size; num++) {
if (num > 0)
- printf(" ");
+ dlt_user_printf(" ");
- printf("%.2x", ((uint8_t *)ptr)[num]);
+ dlt_user_printf("%.2x", ((uint8_t *)ptr)[num]);
}
}
@@ -1754,7 +1754,7 @@ void dlt_log_init(int mode)
logging_handle = fopen(logging_filename, "a");
if (logging_handle == NULL) {
- printf("Internal log file %s cannot be opened!\n", logging_filename);
+ dlt_user_printf("Internal log file %s cannot be opened!\n", logging_filename);
return;
}
}
@@ -1766,6 +1766,29 @@ void dlt_log_free(void)
fclose(logging_handle);
}
+int dlt_user_printf(const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+
+ int ret = 0;
+ switch (logging_mode) {
+ case DLT_LOG_TO_CONSOLE:
+ case DLT_LOG_TO_SYSLOG:
+ case DLT_LOG_TO_FILE:
+ case DLT_LOG_DROPPED:
+ default:
+ ret = vfprintf(stdout, format, args);
+ break;
+ case DLT_LOG_TO_STDERR:
+ ret = vfprintf(stderr, format, args);
+ break;
+ }
+ va_end(args);
+
+ return ret;
+}
+
DltReturnValue dlt_log(int prio, char *s)
{
static const char asSeverity[LOG_DEBUG +
@@ -1789,7 +1812,7 @@ DltReturnValue dlt_log(int prio, char *s)
switch (logging_mode) {
case DLT_LOG_TO_CONSOLE:
/* log to stdout */
- printf(sFormatString,
+ fprintf(stdout, sFormatString,
(unsigned int)sTimeSpec.tv_sec,
(unsigned int)(sTimeSpec.tv_nsec / 1000),
getpid(),
@@ -1797,6 +1820,15 @@ DltReturnValue dlt_log(int prio, char *s)
s);
fflush(stdout);
break;
+ case DLT_LOG_TO_STDERR:
+ /* log to stderr */
+ fprintf(stderr, sFormatString,
+ (unsigned int)sTimeSpec.tv_sec,
+ (unsigned int)(sTimeSpec.tv_nsec / 1000),
+ getpid(),
+ asSeverity[prio],
+ s);
+ break;
case DLT_LOG_TO_SYSLOG:
/* log to syslog */
#if !defined (__WIN32__) && !defined(_MSC_VER)
@@ -3106,7 +3138,7 @@ DltReturnValue dlt_message_print_header(DltMessage *message, char *text, uint32_
return DLT_RETURN_WRONG_PARAMETER;
dlt_message_header(message, text, size, verbose);
- printf("%s\n", text);
+ dlt_user_printf("%s\n", text);
return DLT_RETURN_OK;
}
@@ -3117,9 +3149,9 @@ DltReturnValue dlt_message_print_hex(DltMessage *message, char *text, uint32_t s
return DLT_RETURN_WRONG_PARAMETER;
dlt_message_header(message, text, size, verbose);
- printf("%s ", text);
+ dlt_user_printf("%s ", text);
dlt_message_payload(message, text, size, DLT_OUTPUT_HEX, verbose);
- printf("[%s]\n", text);
+ dlt_user_printf("[%s]\n", text);
return DLT_RETURN_OK;
}
@@ -3130,9 +3162,9 @@ DltReturnValue dlt_message_print_ascii(DltMessage *message, char *text, uint32_t
return DLT_RETURN_WRONG_PARAMETER;
dlt_message_header(message, text, size, verbose);
- printf("%s ", text);
+ dlt_user_printf("%s ", text);
dlt_message_payload(message, text, size, DLT_OUTPUT_ASCII, verbose);
- printf("[%s]\n", text);
+ dlt_user_printf("[%s]\n", text);
return DLT_RETURN_OK;
}
@@ -3143,9 +3175,9 @@ DltReturnValue dlt_message_print_mixed_plain(DltMessage *message, char *text, ui
return DLT_RETURN_WRONG_PARAMETER;
dlt_message_header(message, text, size, verbose);
- printf("%s \n", text);
+ dlt_user_printf("%s \n", text);
dlt_message_payload(message, text, size, DLT_OUTPUT_MIXED_FOR_PLAIN, verbose);
- printf("[%s]\n", text);
+ dlt_user_printf("[%s]\n", text);
return DLT_RETURN_OK;
}
@@ -3156,9 +3188,9 @@ DltReturnValue dlt_message_print_mixed_html(DltMessage *message, char *text, uin
return DLT_RETURN_WRONG_PARAMETER;
dlt_message_header(message, text, size, verbose);
- printf("%s \n", text);
+ dlt_user_printf("%s \n", text);
dlt_message_payload(message, text, size, DLT_OUTPUT_MIXED_FOR_HTML, verbose);
- printf("[%s]\n", text);
+ dlt_user_printf("[%s]\n", text);
return DLT_RETURN_OK;
}
diff --git a/tests/gtest_dlt_common.cpp b/tests/gtest_dlt_common.cpp
index 364afbe..04fdbc6 100644
--- a/tests/gtest_dlt_common.cpp
+++ b/tests/gtest_dlt_common.cpp
@@ -3566,13 +3566,15 @@ TEST(t_dlt_log_set_level, normal)
/* DLT_LOG_TO_CONSOLE=0, */
/* DLT_LOG_TO_SYSLOG=1, */
/* DLT_LOG_TO_FILE=2, */
- /* DLT_LOG_DROPPED=3 */
+ /* DLT_LOG_TO_STDERR=3, */
+ /* DLT_LOG_DROPPED=4 */
/*####################### */
- /* Normal Use-Case, expcect 0-3 */
+ /* Normal Use-Case, expect 0-4 */
EXPECT_NO_THROW(dlt_log_set_level(DLT_LOG_TO_CONSOLE));
EXPECT_NO_THROW(dlt_log_set_level(DLT_LOG_TO_SYSLOG));
EXPECT_NO_THROW(dlt_log_set_level(DLT_LOG_TO_FILE));
+ EXPECT_NO_THROW(dlt_log_set_level(DLT_LOG_TO_STDERR));
EXPECT_NO_THROW(dlt_log_set_level(DLT_LOG_DROPPED));
}
TEST(t_dlt_log_set_level, abnormal)
@@ -3619,7 +3621,8 @@ TEST(t_dlt_log_init, normal)
/* DLT_LOG_TO_CONSOLE=0, */
/* DLT_LOG_TO_SYSLOG=1, */
/* DLT_LOG_TO_FILE=2, */
- /* DLT_LOG_DROPPED=3 */
+ /* DLT_LOG_TO_STDERR=3, */
+ /* DLT_LOG_DROPPED=4 */
/*####################### */
/* Normal Use-Case, exptect 0-3 */
@@ -3628,6 +3631,7 @@ TEST(t_dlt_log_init, normal)
EXPECT_NO_THROW(dlt_log_set_filename("/tmp/dlt.log"));
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_FILE));
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_FILE));
+ EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_STDERR));
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_DROPPED));
}
TEST(t_dlt_log_init, abnormal)
@@ -3651,12 +3655,14 @@ TEST(t_dlt_log_free, normal)
/* DLT_LOG_TO_CONSOLE=0, */
/* DLT_LOG_TO_SYSLOG=1, */
/* DLT_LOG_TO_FILE=2, */
- /* DLT_LOG_DROPPED=3 */
+ /* DLT_LOG_TO_STDERR=3, */
+ /* DLT_LOG_DROPPED=4 */
/*####################### */
/* Normal Use-Case, expected 0 */
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_CONSOLE));
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_SYSLOG));
+ EXPECT_NO_THROW(dlt_log_init(DLT_LOG_TO_STDERR));
EXPECT_NO_THROW(dlt_log_init(DLT_LOG_DROPPED));
}
TEST(t_dlt_log_free, abnormal)