From 1d0806498974e90467535dee84926d295fe256d2 Mon Sep 17 00:00:00 2001 From: laforge Date: Sun, 22 Oct 2006 15:16:22 +0000 Subject: add logging infrastructure to gsm daemon git-svn-id: http://svn.openmoko.org/trunk/src/target/gsm@98 99fdad57-331a-0410-800a-d7fa5415bdb3 --- src/gsmd/Makefile | 373 --------------------------------------------------- src/gsmd/Makefile.am | 2 +- src/gsmd/atcmd.c | 20 +-- src/gsmd/gsmd.c | 11 +- src/gsmd/gsmd.h | 18 ++- src/gsmd/log.c | 83 ++++++++++++ 6 files changed, 120 insertions(+), 387 deletions(-) delete mode 100644 src/gsmd/Makefile create mode 100644 src/gsmd/log.c (limited to 'src/gsmd') diff --git a/src/gsmd/Makefile b/src/gsmd/Makefile deleted file mode 100644 index c447464..0000000 --- a/src/gsmd/Makefile +++ /dev/null @@ -1,373 +0,0 @@ -# Makefile.in generated by automake 1.6.3 from Makefile.am. -# src/gsmd/Makefile. Generated from Makefile.in by configure. - -# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 -# Free Software Foundation, Inc. -# This Makefile.in is free software; the Free Software Foundation -# gives unlimited permission to copy and/or distribute it, -# with or without modifications, as long as this notice is preserved. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY, to the extent permitted by law; without -# even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - - -SHELL = /bin/sh - -srcdir = . -top_srcdir = ../.. - -prefix = /usr/local -exec_prefix = ${prefix} - -bindir = ${exec_prefix}/bin -sbindir = ${exec_prefix}/sbin -libexecdir = ${exec_prefix}/libexec -datadir = ${prefix}/share -sysconfdir = ${prefix}/etc -sharedstatedir = ${prefix}/com -localstatedir = ${prefix}/var -libdir = ${exec_prefix}/lib -infodir = ${prefix}/share/info -mandir = ${prefix}/share/man -includedir = ${prefix}/include -oldincludedir = /usr/include -pkgdatadir = $(datadir)/gsmd -pkglibdir = $(libdir)/gsmd -pkgincludedir = $(includedir)/gsmd -top_builddir = ../.. - -ACLOCAL = ${SHELL} /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/missing --run aclocal-1.6 -AUTOCONF = ${SHELL} /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/missing --run autoconf -AUTOMAKE = ${SHELL} /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/missing --run automake-1.6 -AUTOHEADER = ${SHELL} /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/missing --run autoheader - -am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd -INSTALL = /usr/bin/install -c -INSTALL_PROGRAM = ${INSTALL} -INSTALL_DATA = ${INSTALL} -m 644 -install_sh_DATA = $(install_sh) -c -m 644 -install_sh_PROGRAM = $(install_sh) -c -install_sh_SCRIPT = $(install_sh) -c -INSTALL_SCRIPT = ${INSTALL} -INSTALL_HEADER = $(INSTALL_DATA) -transform = s,x,x, -NORMAL_INSTALL = : -PRE_INSTALL = : -POST_INSTALL = : -NORMAL_UNINSTALL = : -PRE_UNINSTALL = : -POST_UNINSTALL = : -build_alias = -build_triplet = x86_64-unknown-linux-gnu -host_alias = -host_triplet = x86_64-unknown-linux-gnu -target_alias = -target_triplet = x86_64-unknown-linux-gnu - -EXEEXT = -OBJEXT = o -PATH_SEPARATOR = : -AMTAR = ${SHELL} /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/missing --run tar -AR = ar -AS = @AS@ -AWK = gawk -CC = gcc -CXX = g++ -CXXCPP = g++ -E -DEPDIR = .deps -DLLTOOL = @DLLTOOL@ -ECHO = echo -EGREP = /bin/grep -E -F77 = -GCJ = @GCJ@ -GCJFLAGS = @GCJFLAGS@ -INSTALL_STRIP_PROGRAM = ${SHELL} $(install_sh) -c -s -LIBTOOL = $(SHELL) $(top_builddir)/libtool -LIBTOOL_DEPS = ./ltmain.sh -LN_S = ln -s -OBJDUMP = @OBJDUMP@ -PACKAGE = gsmd -RANLIB = ranlib -RC = @RC@ -STRIP = strip -VERSION = 0.0.1 -am__include = include -am__quote = -install_sh = /home/laforge/projects/kommerz/fic/phone/svn/src/target/gsm/install-sh -INCLUDES = $(all_includes) -I$(top_srcdir)/include -AM_CFLAGS = -std=gnu99 - -bin_PROGRAMS = gsmd - -gsmd_SOURCES = gsmd.c atcmd.c select.c vendor_ti.c usock.c unsolicited.c -subdir = src/gsmd -mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs -CONFIG_CLEAN_FILES = -bin_PROGRAMS = gsmd$(EXEEXT) -PROGRAMS = $(bin_PROGRAMS) - -am_gsmd_OBJECTS = gsmd.$(OBJEXT) atcmd.$(OBJEXT) select.$(OBJEXT) \ - vendor_ti.$(OBJEXT) usock.$(OBJEXT) unsolicited.$(OBJEXT) -gsmd_OBJECTS = $(am_gsmd_OBJECTS) -gsmd_LDADD = $(LDADD) -gsmd_DEPENDENCIES = -gsmd_LDFLAGS = - -DEFS = -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"gsmd\" -DVERSION=\"0.0.1\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -DEFAULT_INCLUDES = -I. -I$(srcdir) -CPPFLAGS = -LDFLAGS = -LIBS = -depcomp = $(SHELL) $(top_srcdir)/depcomp -am__depfiles_maybe = depfiles -DEP_FILES = ./$(DEPDIR)/atcmd.Po ./$(DEPDIR)/gsmd.Po \ - ./$(DEPDIR)/select.Po ./$(DEPDIR)/unsolicited.Po \ - ./$(DEPDIR)/usock.Po ./$(DEPDIR)/vendor_ti.Po -COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ - $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \ - $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -CCLD = $(CC) -LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ - $(AM_LDFLAGS) $(LDFLAGS) -o $@ -CFLAGS = -g -O2 -DIST_SOURCES = $(gsmd_SOURCES) -DIST_COMMON = Makefile.am Makefile.in -SOURCES = $(gsmd_SOURCES) - -all: all-am - -.SUFFIXES: -.SUFFIXES: .c .lo .o .obj -$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) - cd $(top_srcdir) && \ - $(AUTOMAKE) --gnu src/gsmd/Makefile -Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status - cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe) -binPROGRAMS_INSTALL = $(INSTALL_PROGRAM) -install-binPROGRAMS: $(bin_PROGRAMS) - @$(NORMAL_INSTALL) - $(mkinstalldirs) $(DESTDIR)$(bindir) - @list='$(bin_PROGRAMS)'; for p in $$list; do \ - p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ - if test -f $$p \ - || test -f $$p1 \ - ; then \ - f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \ - echo " $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f"; \ - $(INSTALL_PROGRAM_ENV) $(LIBTOOL) --mode=install $(binPROGRAMS_INSTALL) $$p $(DESTDIR)$(bindir)/$$f; \ - else :; fi; \ - done - -uninstall-binPROGRAMS: - @$(NORMAL_UNINSTALL) - @list='$(bin_PROGRAMS)'; for p in $$list; do \ - f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \ - echo " rm -f $(DESTDIR)$(bindir)/$$f"; \ - rm -f $(DESTDIR)$(bindir)/$$f; \ - done - -clean-binPROGRAMS: - @list='$(bin_PROGRAMS)'; for p in $$list; do \ - f=`echo $$p|sed 's/$(EXEEXT)$$//'`; \ - echo " rm -f $$p $$f"; \ - rm -f $$p $$f ; \ - done -gsmd$(EXEEXT): $(gsmd_OBJECTS) $(gsmd_DEPENDENCIES) - @rm -f gsmd$(EXEEXT) - $(LINK) $(gsmd_LDFLAGS) $(gsmd_OBJECTS) $(gsmd_LDADD) $(LIBS) - -mostlyclean-compile: - -rm -f *.$(OBJEXT) core *.core - -distclean-compile: - -rm -f *.tab.c - -include ./$(DEPDIR)/atcmd.Po -include ./$(DEPDIR)/gsmd.Po -include ./$(DEPDIR)/select.Po -include ./$(DEPDIR)/unsolicited.Po -include ./$(DEPDIR)/usock.Po -include ./$(DEPDIR)/vendor_ti.Po - -distclean-depend: - -rm -rf ./$(DEPDIR) - -.c.o: - source='$<' object='$@' libtool=no \ - depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' \ - $(CCDEPMODE) $(depcomp) \ - $(COMPILE) -c `test -f '$<' || echo '$(srcdir)/'`$< - -.c.obj: - source='$<' object='$@' libtool=no \ - depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' \ - $(CCDEPMODE) $(depcomp) \ - $(COMPILE) -c `cygpath -w $<` - -.c.lo: - source='$<' object='$@' libtool=yes \ - depfile='$(DEPDIR)/$*.Plo' tmpdepfile='$(DEPDIR)/$*.TPlo' \ - $(CCDEPMODE) $(depcomp) \ - $(LTCOMPILE) -c -o $@ `test -f '$<' || echo '$(srcdir)/'`$< -CCDEPMODE = depmode=gcc3 - -mostlyclean-libtool: - -rm -f *.lo - -clean-libtool: - -rm -rf .libs _libs - -distclean-libtool: - -rm -f libtool -uninstall-info-am: - -ETAGS = etags -ETAGSFLAGS = - -tags: TAGS - -ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - mkid -fID $$unique - -TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ - $(TAGS_FILES) $(LISP) - tags=; \ - here=`pwd`; \ - list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ - unique=`for i in $$list; do \ - if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ - done | \ - $(AWK) ' { files[$$0] = 1; } \ - END { for (i in files) print i; }'`; \ - test -z "$(ETAGS_ARGS)$$tags$$unique" \ - || $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ - $$tags $$unique - -GTAGS: - here=`$(am__cd) $(top_builddir) && pwd` \ - && cd $(top_srcdir) \ - && gtags -i $(GTAGS_ARGS) $$here - -distclean-tags: - -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH -DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) - -top_distdir = ../.. -distdir = $(top_distdir)/$(PACKAGE)-$(VERSION) - -distdir: $(DISTFILES) - @list='$(DISTFILES)'; for file in $$list; do \ - if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ - dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ - if test "$$dir" != "$$file" && test "$$dir" != "."; then \ - dir="/$$dir"; \ - $(mkinstalldirs) "$(distdir)$$dir"; \ - else \ - dir=''; \ - fi; \ - if test -d $$d/$$file; then \ - if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ - cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \ - fi; \ - cp -pR $$d/$$file $(distdir)$$dir || exit 1; \ - else \ - test -f $(distdir)/$$file \ - || cp -p $$d/$$file $(distdir)/$$file \ - || exit 1; \ - fi; \ - done -check-am: all-am -check: check-am -all-am: Makefile $(PROGRAMS) - -installdirs: - $(mkinstalldirs) $(DESTDIR)$(bindir) - -install: install-am -install-exec: install-exec-am -install-data: install-data-am -uninstall: uninstall-am - -install-am: all-am - @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am - -installcheck: installcheck-am -install-strip: - $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ - INSTALL_STRIP_FLAG=-s \ - `test -z '$(STRIP)' || \ - echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install -mostlyclean-generic: - -clean-generic: - -distclean-generic: - -rm -f Makefile $(CONFIG_CLEAN_FILES) - -maintainer-clean-generic: - @echo "This command is intended for maintainers to use" - @echo "it deletes files that may require special tools to rebuild." -clean: clean-am - -clean-am: clean-binPROGRAMS clean-generic clean-libtool mostlyclean-am - -distclean: distclean-am - -distclean-am: clean-am distclean-compile distclean-depend \ - distclean-generic distclean-libtool distclean-tags - -dvi: dvi-am - -dvi-am: - -info: info-am - -info-am: - -install-data-am: - -install-exec-am: install-binPROGRAMS - -install-info: install-info-am - -install-man: - -installcheck-am: - -maintainer-clean: maintainer-clean-am - -maintainer-clean-am: distclean-am maintainer-clean-generic - -mostlyclean: mostlyclean-am - -mostlyclean-am: mostlyclean-compile mostlyclean-generic \ - mostlyclean-libtool - -uninstall-am: uninstall-binPROGRAMS uninstall-info-am - -.PHONY: GTAGS all all-am check check-am clean clean-binPROGRAMS \ - clean-generic clean-libtool distclean distclean-compile \ - distclean-depend distclean-generic distclean-libtool \ - distclean-tags distdir dvi dvi-am info info-am install \ - install-am install-binPROGRAMS install-data install-data-am \ - install-exec install-exec-am install-info install-info-am \ - install-man install-strip installcheck installcheck-am \ - installdirs maintainer-clean maintainer-clean-generic \ - mostlyclean mostlyclean-compile mostlyclean-generic \ - mostlyclean-libtool tags uninstall uninstall-am \ - uninstall-binPROGRAMS uninstall-info-am - -#gsmd_LDADD = ../libgsmd/libgsmd.la -#gsmd_LDFLAGS = -dynamic -# Tell versions [3.59,3.63) of GNU make to not export all variables. -# Otherwise a system limit (for SysV at least) may be exceeded. -.NOEXPORT: diff --git a/src/gsmd/Makefile.am b/src/gsmd/Makefile.am index ae1cc09..2d62662 100644 --- a/src/gsmd/Makefile.am +++ b/src/gsmd/Makefile.am @@ -3,7 +3,7 @@ AM_CFLAGS = -std=gnu99 bin_PROGRAMS = gsmd -gsmd_SOURCES = gsmd.c atcmd.c select.c vendor_ti.c usock.c unsolicited.c +gsmd_SOURCES = gsmd.c atcmd.c select.c vendor_ti.c usock.c unsolicited.c log.c #gsmd_LDADD = ../libgsmd/libgsmd.la #gsmd_LDFLAGS = -dynamic diff --git a/src/gsmd/atcmd.c b/src/gsmd/atcmd.c index ed061e2..c8ae8cd 100644 --- a/src/gsmd/atcmd.c +++ b/src/gsmd/atcmd.c @@ -48,7 +48,7 @@ static inline int llparse_append(struct llparser *llp, char byte) *(llp->cur++) = byte; return 0; } else { - printf("llp->cur too big!!!\n"); + DEBUGP("llp->cur too big!!!\n"); return -EFBIG; } } @@ -160,12 +160,12 @@ static int ml_parse(const char *buf, int len, void *ctx) /* an extended response */ const char *colon = strchr(buf, ':'); if (!colon) { - fprintf(stderr, "no colon in extd response `%s'\n", + gsmd_log(GSMD_ERROR, "no colon in extd response `%s'\n", buf); return -EINVAL; } if (cmd->buf[2] != '+') { - fprintf(stderr, "extd reply to non-extd command?\n"); + gsmd_log(GSMD_ERROR, "extd reply to non-extd command?\n"); return -EINVAL; } @@ -238,7 +238,7 @@ final_cb: g->gfd_uart.when |= GSMD_FD_WRITE; if (!cmd->cb) { - fprintf(stderr, "command without cb!!!\n"); + gsmd_log(GSMD_NOTICE, "command without cb!!!\n"); return -EINVAL; } return cmd->cb(cmd, cmd->ctx); @@ -260,13 +260,13 @@ static int atcmd_select_cb(int fd, unsigned int what, void *data) if (len < 0) { if (errno == EAGAIN) return 0; - DEBUGP("ERROR reading from fd %u: %d (%s)\n", fd, len, + gsmd_log(GSMD_NOTICE, "ERROR reading from fd %u: %d (%s)\n", fd, len, strerror(errno)); return len; } rc = llparse_string(&g->llp, rxbuf, len); if (rc < 0) { - DEBUGP("ERROR during llparse_string: %d\n", rc); + gsmd_log(GSMD_ERROR, "ERROR during llparse_string: %d\n", rc); return rc; } } @@ -279,15 +279,15 @@ static int atcmd_select_cb(int fd, unsigned int what, void *data) len = strlen(pos->buf); rc = write(fd, pos->buf, strlen(pos->buf)); if (rc == 0) { - DEBUGP("write returns 0, aborting\n"); + gsmd_log(GSMD_ERROR, "write returns 0, aborting\n"); break; } else if (rc < 0) { - DEBUGP("error during write to fd %d: %d\n", + gsmd_log(GSMD_ERROR, "error during write to fd %d: %d\n", fd, rc); return rc; } if (rc < len) { - fprintf(stderr, "short write!!! FIXME!\n"); + gsmd_log(GSMD_FATAL, "short write!!! FIXME!\n"); exit(3); } write(fd, "\r", 1); @@ -351,7 +351,7 @@ void atcmd_drain(int fd) struct termios t; rc = tcflush(fd, TCIOFLUSH); rc = tcgetattr(fd, &t); - printf("c_iflag = 0x%08x, c_oflag = 0x%08x, c_cflag = 0x%08x, c_lflag = 0x%08x\n", + DEBUGP("c_iflag = 0x%08x, c_oflag = 0x%08x, c_cflag = 0x%08x, c_lflag = 0x%08x\n", t.c_iflag, t.c_oflag, t.c_cflag, t.c_lflag); t.c_iflag = t.c_oflag = 0; rc = tcsetattr(fd, TCSANOW, &t); diff --git a/src/gsmd/gsmd.c b/src/gsmd/gsmd.c index 9267977..e6d9982 100644 --- a/src/gsmd/gsmd.c +++ b/src/gsmd/gsmd.c @@ -113,6 +113,7 @@ static struct option opts[] = { { "help", 0, NULL, 'h' }, { "device", 1, NULL, 'p' }, { "speed", 1, NULL, 's' }, + { "logfile", 1, NULL, 'l' }, }; static void print_help(void) @@ -125,6 +126,7 @@ static void print_help(void) "\t-h\t--help\t\tDisplay this help message\n" "\t-p dev\t--device dev\tSpecify serial device to be used\n" "\t-s spd\t--speed spd\tSpecify speed in bps (9600,38400,115200,...)\n" + "\t-l file\t--logfile file\tSpecify a logfile to log to\n" ); } @@ -135,9 +137,10 @@ int main(int argc, char **argv) int daemonize = 0; int bps = 115200; char *device = "/dev/ttyUSB0"; + char *logfile = "syslog"; /*FIXME: parse commandline, set daemonize, device, ... */ - while ((argch = getopt_long(argc, argv, "Vdhp:s:", opts, NULL)) != -1) { + while ((argch = getopt_long(argc, argv, "Vdhp:s:l:", opts, NULL)) != -1) { switch (argch) { case 'V': /* FIXME */ @@ -156,6 +159,12 @@ int main(int argc, char **argv) case 's': bps = atoi(optarg); break; + case 'l': + if (gsmdlog_init(optarg)) { + fprintf(stderr, "can't open logfile `%s'\n", optarg); + exit(2); + } + break; } } diff --git a/src/gsmd/gsmd.h b/src/gsmd/gsmd.h index 7a94ef7..460e9d4 100644 --- a/src/gsmd/gsmd.h +++ b/src/gsmd/gsmd.h @@ -65,5 +65,19 @@ struct gsmd_user { u_int32_t subscriptions; /* bitmaks of subscribed event groups */ }; -#define DEBUGP(x, args ...) printf("%s:%s(%d):" x, __FILE__, __FUNCTION__, __LINE__, ## args) -#endif +#define GSMD_DEBUG 1 /* debugging information */ +#define GSMD_INFO 3 +#define GSMD_NOTICE 5 /* abnormal/unexpected condition */ +#define GSMD_ERROR 7 /* error condition, requires user action */ +#define GSMD_FATAL 8 /* fatal, program aborted */ + +extern int gsmdlog_init(const char *path); +/* write a message to the daemons' logfile */ +void __gsmd_log(int level, const char *file, int line, const char *message, ...); +/* macro for logging including filename and line number */ +#define gsmd_log(level, format, args...) \ + __gsmd_log(level, __FILE__, __LINE__, format, ## args) + +#define DEBUGP(x, args ...) gsmd_log(GSMD_DEBUG, x, ## args) + +#endif /* __GSMD_H */ diff --git a/src/gsmd/log.c b/src/gsmd/log.c new file mode 100644 index 0000000..671916e --- /dev/null +++ b/src/gsmd/log.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "gsmd.h" + +static FILE *logfile; +static FILE syslog_dummy; +static int loglevel; + +static int gsmd2syslog[] = { + [GSMD_DEBUG] = LOG_DEBUG, + [GSMD_INFO] = LOG_INFO, + [GSMD_NOTICE] = LOG_NOTICE, + [GSMD_ERROR] = LOG_ERR, + [GSMD_FATAL] = LOG_CRIT, +}; + +static inline int gsmd2syslog_level(int level) +{ + if (level >= ARRAY_SIZE(gsmd2syslog)) + return LOG_ERR; + + return gsmd2syslog[level]; +} + +void __gsmd_log(int level, const char *file, int line, const char *format, ...) +{ + char *timestr; + va_list ap; + time_t tm; + FILE *outfd; + + if (level < loglevel) + return; + + if (logfile == &syslog_dummy) { + va_start(ap, format); + vsyslog(gsmd2syslog_level(level), format, ap); + va_end(ap); + } else { + if (logfile) + outfd = logfile; + else + outfd = stderr; + + tm = time(NULL); + timestr = ctime(&tm); + timestr[strlen(timestr)-1] = '\0'; + fprintf(outfd, "%s <%1.1d> %s:%d ", timestr, level, file, line); + + va_start(ap, format); + vfprintf(outfd, format, ap); + va_end(ap); + + fflush(outfd); + } +} + +int gsmdlog_init(const char *path) +{ + + if (!strcmp(path, "syslog")) { + logfile = &syslog_dummy; + openlog("gsmd", 0, LOG_DAEMON); + } else { + logfile = fopen(path, "a+"); + } + + if (logfile == NULL) + return -1; + + gsmd_log(LOG_INFO, "logfile successfully opened\n"); + + return 0; +} -- cgit v1.2.3