aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2017-07-23 16:48:59 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2017-07-23 16:48:59 +0200
commit9f81c9672bd1755e24b6ce55cc9f04f18792db7d (patch)
treea9c2998117e222281d236c0c86cea097436fcf80
parent15396d2f62b9efb15712cdec4c38d697d6de8e9a (diff)
downloadembedlog-9f81c9672bd1755e24b6ce55cc9f04f18792db7d.tar.gz
embedlog-9f81c9672bd1755e24b6ce55cc9f04f18792db7d.tar.bz2
embedlog-9f81c9672bd1755e24b6ce55cc9f04f18792db7d.zip
Added el_print_error function (perror equivalent)
-rw-r--r--include/embedlog.h1
-rw-r--r--src/el-print.c86
2 files changed, 74 insertions, 13 deletions
diff --git a/include/embedlog.h b/include/embedlog.h
index f86aa03..6c9bf4c 100644
--- a/include/embedlog.h
+++ b/include/embedlog.h
@@ -63,6 +63,7 @@ int el_output_disable(enum el_output);
int el_option(enum el_option, int);
int el_print(enum el_level, const char *, size_t, const char *, ...);
int el_print_mem(enum el_level, const char *, size_t, const void *, size_t);
+int el_print_error(enum el_level, const char *, size_t, const char *, ...);
#endif
diff --git a/src/el-print.c b/src/el-print.c
index db9a38b..3850be0 100644
--- a/src/el-print.c
+++ b/src/el-print.c
@@ -17,10 +17,12 @@
#include <time.h>
#endif
+#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
+
/* ==== private variables =================================================== */
@@ -381,14 +383,11 @@ static void el_puts
}
-/* ==== public functions ==================================================== */
-
-
/* ==========================================================================
- Prints message formated by 'fmt' and '...' with timestamp, 'file' and
- line number 'num' information, with specified 'level' into configured
- outputs. Function allocates on callers heap EL_BUF_MAX of memory. If
- log message is longer than available buffer, it will be truncated and
+ Prints message formated by 'fmt' and 'ap' with timestamp, 'file' and
+ line number 'num' information, with specified 'level' into configured
+ outputs. Function allocates on callers heap EL_BUF_MAX of memory. If
+ log message is longer than available buffer, it will be truncated and
part of message will be lost.
errno:
@@ -399,19 +398,18 @@ static void el_puts
========================================================================== */
-int el_print
+static int el_printv
(
enum el_level level, /* log level to print message with */
const char *file, /* file name where log is printed */
size_t num, /* line number where log is printed */
const char *fmt, /* message format (see printf (3)) */
- ... /* additional parameters for fmt */
+ va_list ap /* additional parameters for fmt */
)
{
char buf[EL_BUF_MAX + 2]; /* buffer for message to print */
size_t w; /* bytes written to buf */
size_t flen; /* length of the parsed fmt output */
- va_list va; /* arguments '...' for fmt */
int e; /* error code */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
@@ -447,9 +445,7 @@ int el_print
* add requested log from format, we add + 1 to include null termination
*/
- va_start(va, fmt);
- flen = vsnprintf(buf + w, EL_LOG_MAX + 1, fmt, va);
- va_end(va);
+ flen = vsnprintf(buf + w, EL_LOG_MAX + 1, fmt, ap);
if (flen > EL_LOG_MAX)
{
@@ -488,3 +484,67 @@ int el_print
return 0;
}
+
+
+/* ==== public functions ==================================================== */
+
+
+/* ==========================================================================
+ calls el_print with 'fmt' and '...' parameters, but additionaly prints
+ information about errno. Functionaly it is similar to perror function
+ ========================================================================== */
+
+
+int el_print_error
+(
+ enum el_level level, /* log level to print message with */
+ const char *file, /* file name where log is printed */
+ size_t num, /* line number where log is printed */
+ const char *fmt, /* message format (see printf (3)) */
+ ... /* additional parameters for fmt */
+)
+{
+ va_list ap; /* arguments '...' for 'fmt' */
+ int rc; /* return code from el_printfv() */
+ unsigned long e; /* errno from upper layer */
+ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+
+ e = errno;
+
+ va_start(ap, fmt);
+ rc = el_printv(level, file, num, fmt, ap);
+ rc |= el_print(level, file, num,
+ "errno num: %lu, strerror: %s", e, strerror(e));
+ va_end(ap);
+}
+
+
+/* ==========================================================================
+ simply calls el_printv with '...' converted to 'va_list'
+ ========================================================================== */
+
+
+int el_print
+(
+ enum el_level level, /* log level to print message with */
+ const char *file, /* file name where log is printed */
+ size_t num, /* line number where log is printed */
+ const char *fmt, /* message format (see printf (3)) */
+ ... /* additional parameters for fmt */
+)
+{
+ va_list ap; /* arguments '...' for 'fmt' */
+ int rc; /* return code from el_printfv() */
+ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+
+ va_start(ap, fmt);
+ rc = el_printv(level, file, num, fmt, ap);
+ va_end(ap);
+
+ return rc;
+}
+
+
+