Change in libosmocore[master]: Log: add stdout target

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Max gerrit-no-reply at lists.osmocom.org
Tue Dec 4 11:46:16 UTC 2018


Max has uploaded this change for review. ( https://gerrit.osmocom.org/12093


Change subject: Log: add stdout target
......................................................................

Log: add stdout target

This is useful for code testing internal library functions which allows
to automatically fail tests due to output mismatch.

As a side-effect this also simplifies #if-#else logic due to both
_stdout() and _stderr() now being simple wrappers around static
function.

No user-visible changes are introduced because stdout is ignored by vty
code to avoid messing up vty output.

Change-Id: Ia786361f5f687e43b27d87a45b4630bca58bcfe8
---
M include/osmocom/core/logging.h
M src/logging.c
M src/vty/logging_vty.c
3 files changed, 37 insertions(+), 7 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/93/12093/1

diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 295e5a8..4a835ff 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -218,6 +218,7 @@
 	LOG_TGT_TYPE_SYSLOG,	/*!< syslog based logging */
 	LOG_TGT_TYPE_FILE,	/*!< text file logging */
 	LOG_TGT_TYPE_STDERR,	/*!< stderr logging */
+	LOG_TGT_TYPE_STDOUT,	/*!< stdout logging */
 	LOG_TGT_TYPE_STRRB,	/*!< osmo_strrb-backed logging */
 	LOG_TGT_TYPE_GSMTAP,	/*!< GSMTAP network logging */
 };
@@ -361,6 +362,7 @@
 struct log_target *log_target_create(void);
 void log_target_destroy(struct log_target *target);
 struct log_target *log_target_create_stderr(void);
+struct log_target *log_target_create_stdout(void);
 struct log_target *log_target_create_file(const char *fname);
 struct log_target *log_target_create_syslog(const char *ident, int option,
 					    int facility);
diff --git a/src/logging.c b/src/logging.c
index 908ba54..31345d1 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -810,24 +810,45 @@
 	return target;
 }
 
-/*! Create the STDERR log target
- *  \returns dynamically-allocated \ref log_target for STDERR */
-struct log_target *log_target_create_stderr(void)
+static struct log_target *_log_target_create_std(FILE *out, enum log_target_type lt)
 {
-/* since C89/C99 says stderr is a macro, we can safely do this! */
-#if !EMBEDDED && defined(stderr)
+#if !EMBEDDED
 	struct log_target *target;
 
 	target = log_target_create();
 	if (!target)
 		return NULL;
 
-	target->type = LOG_TGT_TYPE_STDERR;
-	target->tgt_file.out = stderr;
+	target->type = lt;
+	target->tgt_file.out = out;
 	target->output = _file_output;
 	return target;
 #else
 	return NULL;
+#endif
+}
+
+/*! Create the STDOUT log target
+ *  \returns dynamically-allocated \ref log_target for STDOUT */
+struct log_target *log_target_create_stdout(void)
+{
+/* since C89/C99 says stdout is a macro, we can safely do this! */
+#if defined(stdout)
+	return _log_target_create_std(stdout, LOG_TGT_TYPE_STDOUT);
+#else
+	return NULL;
+#endif /* stdout */
+}
+
+/*! Create the STDERR log target
+ *  \returns dynamically-allocated \ref log_target for STDERR */
+struct log_target *log_target_create_stderr(void)
+{
+/* since C89/C99 says stderr is a macro, we can safely do this! */
+#if defined(stderr)
+	return _log_target_create_std(stderr, LOG_TGT_TYPE_STDERR);
+#else
+	return NULL;
 #endif /* stderr */
 }
 
@@ -900,6 +921,10 @@
 		/* don't close stderr */
 		if (target->tgt_file.out != stderr)
 #endif
+#ifdef stdout
+		/* don't close stdout */
+		if (target->tgt_file.out != stdout)
+#endif
 		{
 			fclose(target->tgt_file.out);
 			target->tgt_file.out = NULL;
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index f3e1419..e2d2b57 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -877,6 +877,9 @@
 	case LOG_TGT_TYPE_STDERR:
 		vty_out(vty, "log stderr%s", VTY_NEWLINE);
 		break;
+	case LOG_TGT_TYPE_STDOUT:
+		/* we don't support vty logging to stdout to avoid messing up vty prompt and user input */
+		break;
 	case LOG_TGT_TYPE_SYSLOG:
 #ifdef HAVE_SYSLOG_H
 		vty_out(vty, "log syslog %s%s",

-- 
To view, visit https://gerrit.osmocom.org/12093
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia786361f5f687e43b27d87a45b4630bca58bcfe8
Gerrit-Change-Number: 12093
Gerrit-PatchSet: 1
Gerrit-Owner: Max <msuraev at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181204/13989ff2/attachment.htm>


More information about the gerrit-log mailing list