diff options
author | Nikola Pajkovsky <npajkovs@redhat.com> | 2010-08-05 17:04:59 +0200 |
---|---|---|
committer | Nikola Pajkovsky <npajkovs@redhat.com> | 2010-08-10 09:56:27 +0200 |
commit | f926010c0f67582bcb37d1221cfe09743248b148 (patch) | |
tree | ff31b19eff3bc831a6b482b336b2116e555bdcb5 | |
parent | 952420f6b5843e7f16474e3fd288bd8585c05373 (diff) | |
download | abrt-f926010c0f67582bcb37d1221cfe09743248b148.tar.gz abrt-f926010c0f67582bcb37d1221cfe09743248b148.tar.xz abrt-f926010c0f67582bcb37d1221cfe09743248b148.zip |
pure C for logging, read_wirte and xfuncs
Signed-off-by: Nikola Pajkovsky <npajkovs@redhat.com>
-rw-r--r-- | inc/abrtlib.h | 75 | ||||
-rw-r--r-- | inc/xfuncs.h | 12 | ||||
-rw-r--r-- | lib/Utils/Makefile.am | 6 | ||||
-rw-r--r-- | lib/Utils/logging.c (renamed from lib/Utils/logging.cpp) | 3 | ||||
-rw-r--r-- | lib/Utils/logging.h | 83 | ||||
-rw-r--r-- | lib/Utils/read_write.c (renamed from lib/Utils/read_write.cpp) | 3 | ||||
-rw-r--r-- | lib/Utils/read_write.h | 49 | ||||
-rw-r--r-- | lib/Utils/xfuncs.c (renamed from lib/Utils/xfuncs.cpp) | 26 |
8 files changed, 172 insertions, 85 deletions
diff --git a/inc/abrtlib.h b/inc/abrtlib.h index 0e650059..c8025e70 100644 --- a/inc/abrtlib.h +++ b/inc/abrtlib.h @@ -56,45 +56,7 @@ int vdprintf(int d, const char *format, va_list ap); #undef ARRAY_SIZE #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0]))) - -/* Logging */ -enum { - LOGMODE_NONE = 0, - LOGMODE_STDIO = (1 << 0), - LOGMODE_SYSLOG = (1 << 1), - LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO, - LOGMODE_CUSTOM = (1 << 2), -}; -extern void (*g_custom_logger)(const char*); -extern const char *msg_prefix; -extern const char *msg_eol; -extern int logmode; -extern int xfunc_error_retval; -void xfunc_die(void) NORETURN; -void log_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); -/* It's a macro, not function, since it collides with log() from math.h */ -#undef log -#define log(...) log_msg(__VA_ARGS__) -/* error_msg family will use g_custom_logger. log_msg does not. */ -void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); -void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); -/* Reports error message with libc's errno error description attached. */ -void perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); -void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); -void perror_nomsg_and_die(void) NORETURN; -void perror_nomsg(void); -void verror_msg(const char *s, va_list p, const char *strerr); -void die_out_of_memory(void) NORETURN; - -/* Verbosity level */ -extern int g_verbose; -/* VERB1 log("what you sometimes want to see, even on a production box") */ -#define VERB1 if (g_verbose >= 1) -/* VERB2 log("debug message, not going into insanely small details") */ -#define VERB2 if (g_verbose >= 2) -/* VERB3 log("lots and lots of details") */ -#define VERB3 if (g_verbose >= 3) -/* there is no level > 3 */ +#include "logging.h" char* skip_whitespace(const char *s); char* skip_non_whitespace(const char *s); @@ -118,16 +80,7 @@ int xatoi(const char *numstr); * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc */ int xatoi_u(const char *numstr); - -extern ssize_t safe_read(int fd, void *buf, size_t count); -// NB: will return short read on error, not -1, -// if some data was read before error occurred -extern ssize_t full_read(int fd, void *buf, size_t count); -extern void xread(int fd, void *buf, size_t count); -extern ssize_t safe_write(int fd, const void *buf, size_t count); -// NB: will return short write on error, not -1, -// if some data was written before error occurred -extern ssize_t full_write(int fd, const void *buf, size_t count); +#include "read_write.h" /* copyfd_XX print read/write errors and return -1 if they occur */ enum { @@ -247,4 +200,28 @@ std::string to_string(T x) void parse_args(const char *psArgs, vector_string_t& pArgs, int quote = -1); void parse_release(const char *pRelease, char **product, char **version); +// TODO: npajkovs: full rewrite ssprintf -> xasprintf +static inline std::string ssprintf(const char *format, ...) +{ + va_list p; + char *string_ptr; + + va_start(p, format); + string_ptr = xvasprintf(format, p); + va_end(p); + + std::string res = string_ptr; + free(string_ptr); + return res; +} + +static inline std::string concat_path_file(const char *path, const char *filename) +{ + char *lc; + + while (*filename == '/') + filename++; + lc = last_char_is(path, '/'); + return ssprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename); +} #endif diff --git a/inc/xfuncs.h b/inc/xfuncs.h index de785c8e..619adb13 100644 --- a/inc/xfuncs.h +++ b/inc/xfuncs.h @@ -19,10 +19,17 @@ #ifndef ABRT_XFUNCS_H #define ABRT_XFUNCS_H +#include <stdio.h> +#include <stdlib.h> + #include <sys/socket.h> #include <sys/stat.h> #include <dirent.h> #include <stdbool.h> +#include <fcntl.h> +#include <pwd.h> + +#include "logging.h" #ifdef __cplusplus extern "C" { @@ -88,9 +95,4 @@ extern const char *get_home_dir(uid_t uid); } #endif -#ifdef __cplusplus -std::string ssprintf(const char *format, ...); -std::string concat_path_file(const char *path, const char *filename); -#endif - #endif diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am index 206b825a..88aab4d2 100644 --- a/lib/Utils/Makefile.am +++ b/lib/Utils/Makefile.am @@ -10,10 +10,10 @@ AM_YFLAGS = --verbose # xconnect.cpp libABRTUtils_la_SOURCES = \ - xfuncs.cpp \ + xfuncs.c \ encbase64.cpp \ - read_write.cpp \ - logging.cpp \ + read_write.c read_write.h \ + logging.c logging.h \ copyfd.cpp \ daemon.cpp \ skip_whitespace.cpp \ diff --git a/lib/Utils/logging.cpp b/lib/Utils/logging.c index 0a50882f..a6eaa50f 100644 --- a/lib/Utils/logging.cpp +++ b/lib/Utils/logging.c @@ -22,8 +22,7 @@ * */ -#include "abrtlib.h" -#include <syslog.h> +#include "logging.h" int xfunc_error_retval = EXIT_FAILURE; int g_verbose; diff --git a/lib/Utils/logging.h b/lib/Utils/logging.h new file mode 100644 index 00000000..f682feb0 --- /dev/null +++ b/lib/Utils/logging.h @@ -0,0 +1,83 @@ +/* + Copyright (C) 2010 ABRT team + Copyright (C) 2010 RedHat Inc + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef LOGGING_H +#define LOGGING_H + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <sys/syslog.h> + +#include "read_write.h" +#include "xfuncs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define NORETURN __attribute__ ((noreturn)) + + +enum { + LOGMODE_NONE = 0, + LOGMODE_STDIO = (1 << 0), + LOGMODE_SYSLOG = (1 << 1), + LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO, + LOGMODE_CUSTOM = (1 << 2), +}; + +extern void (*g_custom_logger)(const char*); +extern const char *msg_prefix; +extern const char *msg_eol; +extern int logmode; +extern int xfunc_error_retval; +void xfunc_die(void) NORETURN; +void log_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); +/* It's a macro, not function, since it collides with log() from math.h */ +#undef log +#define log(...) log_msg(__VA_ARGS__) +/* error_msg family will use g_custom_logger. log_msg does not. */ +void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); +void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); +/* Reports error message with libc's errno error description attached. */ +void perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); +void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); +void perror_nomsg_and_die(void) NORETURN; +void perror_nomsg(void); +void verror_msg(const char *s, va_list p, const char *strerr); +void die_out_of_memory(void) NORETURN; + +/* Verbosity level */ +extern int g_verbose; +/* VERB1 log("what you sometimes want to see, even on a production box") */ +#define VERB1 if (g_verbose >= 1) +/* VERB2 log("debug message, not going into insanely small details") */ +#define VERB2 if (g_verbose >= 2) +/* VERB3 log("lots and lots of details") */ +#define VERB3 if (g_verbose >= 3) +/* there is no level > 3 */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/Utils/read_write.cpp b/lib/Utils/read_write.c index 38907aa9..3f2133e6 100644 --- a/lib/Utils/read_write.cpp +++ b/lib/Utils/read_write.c @@ -3,7 +3,8 @@ * * Licensed under GPLv2 or later, see file COPYING in this tarball for details. */ -#include "abrtlib.h" + +#include "read_write.h" ssize_t safe_read(int fd, void *buf, size_t count) { diff --git a/lib/Utils/read_write.h b/lib/Utils/read_write.h new file mode 100644 index 00000000..5a351869 --- /dev/null +++ b/lib/Utils/read_write.h @@ -0,0 +1,49 @@ +/* + Copyright (C) 2010 ABRT team + Copyright (C) 2010 RedHat Inc + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef READ_WRITE_H +#define READ_WRITE_H + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> + +#include "logging.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +// NB: will return short read on error, not -1, +// if some data was read before error occurred +void xread(int fd, void *buf, size_t count); + +ssize_t safe_read(int fd, void *buf, size_t count); +ssize_t safe_write(int fd, const void *buf, size_t count); + +ssize_t full_read(int fd, void *buf, size_t count); +ssize_t full_write(int fd, const void *buf, size_t count); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/Utils/xfuncs.cpp b/lib/Utils/xfuncs.c index e2f9b4d3..bd6efec8 100644 --- a/lib/Utils/xfuncs.cpp +++ b/lib/Utils/xfuncs.c @@ -22,7 +22,7 @@ * */ -#include "abrtlib.h" +#include "xfuncs.h" /* Turn on nonblocking I/O on a fd */ int ndelay_on(int fd) @@ -204,20 +204,6 @@ char* xasprintf(const char *format, ...) return string_ptr; } -std::string ssprintf(const char *format, ...) -{ - va_list p; - char *string_ptr; - - va_start(p, format); - string_ptr = xvasprintf(format, p); - va_end(p); - - std::string res = string_ptr; - free(string_ptr); - return res; -} - void xsetenv(const char *key, const char *value) { if (setenv(key, value, 1)) @@ -367,16 +353,6 @@ char *last_char_is(const char *s, int c) return NULL; } -std::string concat_path_file(const char *path, const char *filename) -{ - char *lc; - - while (*filename == '/') - filename++; - lc = last_char_is(path, '/'); - return ssprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename); -} - bool string_to_bool(const char *s) { if (s[0] == '1' && s[1] == '\0') |