From f534061fc8230481a3ca40abc1d1e9b79d3c4976 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Sat, 15 Feb 2014 21:42:29 +0000 Subject: [PATCH] merged plib --- src/Makefile.am | 5 +- src/Makefile.in | 15 +++--- src/cache.c | 2 +- src/fcache.c | 2 +- src/html.c | 2 +- src/http.c | 2 +- src/log.c | 136 ------------------------------------------------ src/log.h | 41 --------------- src/lp.c | 22 +------- src/lp.h | 2 - src/lp_json.c | 3 +- src/lp_ws.c | 5 +- src/main.c | 4 +- src/plog.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/plog.h | 44 ++++++++++++++++ src/ppastats.c | 2 +- src/ptime.c | 51 ++++++++++++++++++ src/ptime.h | 29 +++++++++++ 18 files changed, 307 insertions(+), 217 deletions(-) delete mode 100644 src/log.c delete mode 100644 src/log.h create mode 100644 src/plog.c create mode 100644 src/plog.h create mode 100644 src/ptime.c create mode 100644 src/ptime.h diff --git a/src/Makefile.am b/src/Makefile.am index 0173371..96f0c4e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,14 +14,15 @@ ppastats_SOURCES = \ html.h html.c\ http.h http.c\ list.h list.c\ - log.h log.c\ lp.h lp.c\ lp_json.h lp_json.c\ lp_ws.h lp_ws.c\ main.c\ + plog.h plog.c\ ppastats.h ppastats.c\ pio.h pio.c\ - pstr.h pstr.c + pstr.h pstr.c\ + ptime.h ptime.c dist_man_MANS = ppastats.1 diff --git a/src/Makefile.in b/src/Makefile.in index fb6c621..af1af11 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -98,9 +98,10 @@ CONFIG_CLEAN_VPATH_FILES = am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am_ppastats_OBJECTS = cache.$(OBJEXT) fcache.$(OBJEXT) html.$(OBJEXT) \ - http.$(OBJEXT) list.$(OBJEXT) log.$(OBJEXT) lp.$(OBJEXT) \ - lp_json.$(OBJEXT) lp_ws.$(OBJEXT) main.$(OBJEXT) \ - ppastats.$(OBJEXT) pio.$(OBJEXT) pstr.$(OBJEXT) + http.$(OBJEXT) list.$(OBJEXT) lp.$(OBJEXT) lp_json.$(OBJEXT) \ + lp_ws.$(OBJEXT) main.$(OBJEXT) plog.$(OBJEXT) \ + ppastats.$(OBJEXT) pio.$(OBJEXT) pstr.$(OBJEXT) \ + ptime.$(OBJEXT) ppastats_OBJECTS = $(am_ppastats_OBJECTS) ppastats_LDADD = $(LDADD) AM_V_P = $(am__v_P_@AM_V@) @@ -320,14 +321,15 @@ ppastats_SOURCES = \ html.h html.c\ http.h http.c\ list.h list.c\ - log.h log.c\ lp.h lp.c\ lp_json.h lp_json.c\ lp_ws.h lp_ws.c\ main.c\ + plog.h plog.c\ ppastats.h ppastats.c\ pio.h pio.c\ - pstr.h pstr.c + pstr.h pstr.c\ + ptime.h ptime.c dist_man_MANS = ppastats.1 all: all-am @@ -422,14 +424,15 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/http.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp_json.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lp_ws.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plog.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ppastats.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pstr.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ptime.Po@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/cache.c b/src/cache.c index 8053421..a25c47d 100644 --- a/src/cache.c +++ b/src/cache.c @@ -25,7 +25,7 @@ #include #include "cache.h" -#include "log.h" +#include #include "ppastats.h" diff --git a/src/fcache.c b/src/fcache.c index 852d977..1c9fe66 100644 --- a/src/fcache.c +++ b/src/fcache.c @@ -26,7 +26,7 @@ #include #include "fcache.h" -#include "log.h" +#include #include static const char *cache_dir; diff --git a/src/html.c b/src/html.c index 379a9c1..408c18e 100644 --- a/src/html.c +++ b/src/html.c @@ -30,12 +30,12 @@ #include #include "html.h" -#include "log.h" #include "lp.h" #include #include "lp_ws.h" #include "ppastats.h" #include +#include #include static const char *footer; diff --git a/src/http.c b/src/http.c index d6c7983..4370f25 100644 --- a/src/http.c +++ b/src/http.c @@ -28,7 +28,7 @@ #include "fcache.h" #include "http.h" -#include "log.h" +#include static const int DEFAULT_FETCH_RETRIES = 10; diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 813cf04..0000000 --- a/src/log.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright (C) 2010-2014 jeanfi@gmail.com - * - * 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 - */ -#include -#include -#define _(str) gettext(str) - -#include -#include -#include - -#include "log.h" - -static FILE *file; -int log_level = LOG_WARN; - -void log_open(const char *path) -{ - file = fopen(path, "a"); - - if (!file) - log_printf(LOG_ERR, _("Cannot open log file: %s"), path); -} - -void log_close() -{ - if (!file) - return ; - - fclose(file); - - file = NULL; -} - - -#define LOG_BUFFER 4096 -static void vlogf(int lvl, const char *fmt, va_list ap) -{ - struct timeval tv; - char buffer[1 + LOG_BUFFER]; - char *lvl_str; - FILE *stdf; - - if (lvl > log_level) - return ; - - vsnprintf(buffer, LOG_BUFFER, fmt, ap); - buffer[LOG_BUFFER] = '\0'; - - if (gettimeofday(&tv, NULL) != 0) - timerclear(&tv); - - switch (lvl) { - case LOG_WARN: - lvl_str = "[WARN]"; - break; - case LOG_ERR: - lvl_str = "[ERR]"; - break; - case LOG_DEBUG: - lvl_str = "[DEBUG]"; - break; - case LOG_INFO: - lvl_str = "[INFO]"; - break; - default: - lvl_str = "[??]"; - } - - if (file) { - fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); - fflush(file); - } - - if (lvl == LOG_WARN || lvl == LOG_ERR) - stdf = stderr; - else - stdf = stdout; - - fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); - fflush(stdf); -} - -void log_printf(int lvl, const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vlogf(lvl, fmt, ap); - va_end(ap); -} - -void log_debug(const char *fmt, ...) -{ - va_list ap; - - if (log_level < LOG_DEBUG) - return ; - - va_start(ap, fmt); - vlogf(LOG_DEBUG, fmt, ap); - va_end(ap); -} - -void log_err(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vlogf(LOG_ERR, fmt, ap); - va_end(ap); -} - -void log_warn(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vlogf(LOG_WARN, fmt, ap); - va_end(ap); -} diff --git a/src/log.h b/src/log.h deleted file mode 100644 index 6beb35e..0000000 --- a/src/log.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (C) 2010-2014 jeanfi@gmail.com - * - * 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 _PPASTATS_LOG_H_ -#define _PPASTATS_LOG_H_ - -enum log_level { - LOG_ERR , - LOG_WARN, - LOG_INFO, - LOG_DEBUG -}; - -void log_open(const char *path); - -void log_printf(int lvl, const char *fmt, ...); -void log_debug(const char *fmt, ...); -void log_err(const char *fmt, ...); -void log_warn(const char *fmt, ...); - -void log_close(); - -/* level of the log file. */ -extern int log_level; - -#endif diff --git a/src/lp.c b/src/lp.c index e23b77b..e738a6c 100644 --- a/src/lp.c +++ b/src/lp.c @@ -23,27 +23,7 @@ #include "list.h" #include "lp.h" - -char *time_to_str(time_t t) -{ - char *str; - struct tm *tm; - size_t ret; - - tm = localtime(&t); - - if (!tm) - return NULL; - - str = malloc(strlen("YYYY-MM-DDThh:mm:ss") + 1); - ret = strftime(str, strlen("YYYY-MM-DDThh:mm:ss") + 1, "%FT%T", tm); - - if (ret) - return str; - - free(str); - return NULL; -} +#include struct distro_series *distro_series_new(const char *name, const char *version, diff --git a/src/lp.h b/src/lp.h index 394aa17..47c861f 100644 --- a/src/lp.h +++ b/src/lp.h @@ -91,8 +91,6 @@ struct bpph **bpph_list_append_list(struct bpph **list1, struct bpph **list2); char *get_archive_url(const char *owner, const char *ppa); -char *time_to_str(time_t t); - time_t ddts_get_last_date(struct daily_download_total **); struct daily_download_total ** diff --git a/src/lp_json.c b/src/lp_json.c index c3345fd..0643a1b 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -25,6 +25,7 @@ #include "lp_json.h" #include "lp_ws.h" +#include static time_t json_to_time(json_object *json) { @@ -51,7 +52,7 @@ static json_object *time_to_json(time_t t) { char *str; - str = time_to_str(t); + str = time_to_str(&t); if (str) return json_object_new_string(str); diff --git a/src/lp_ws.c b/src/lp_ws.c index 8e66167..0dce140 100644 --- a/src/lp_ws.c +++ b/src/lp_ws.c @@ -32,10 +32,11 @@ #include "fcache.h" #include "http.h" #include "list.h" -#include "log.h" #include "lp_ws.h" #include "lp_json.h" +#include #include "ppastats.h" +#include /** Default ws.size value for the getPublishedBinaries request. */ static const int DEFAULT_WS_SIZE = 150; @@ -119,7 +120,7 @@ static char *get_last_creation_date(struct bpph **list) } if (last) - return time_to_str(last); + return time_to_str(&last); else return NULL; } diff --git a/src/main.c b/src/main.c index e1f17d5..58cb028 100644 --- a/src/main.c +++ b/src/main.c @@ -28,9 +28,9 @@ #include "cache.h" #include "html.h" #include "http.h" -#include "log.h" #include "lp_ws.h" #include "config.h" +#include #include "ppastats.h" static const char *program_name; @@ -203,6 +203,8 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } + log_open("ppastats.log"); + owner = argv[optind]; ppa = argv[optind+1]; diff --git a/src/plog.c b/src/plog.c new file mode 100644 index 0000000..be6d09e --- /dev/null +++ b/src/plog.c @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2010-2014 jeanfi@gmail.com + * + * 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 + */ +#define _LARGEFILE_SOURCE 1 +#include "config.h" + +#include +#include +#define _(str) gettext(str) + +#include +#include +#include +#include +#include + +#include +#include + +static FILE *file; +int log_level = LOG_WARN; + +void log_open(const char *path) +{ + file = fopen(path, "a"); + + if (!file) + log_printf(LOG_ERR, _("Cannot open log file: %s"), path); +} + +void log_close() +{ + if (!file) + return ; + + fclose(file); + + file = NULL; +} + + +#define LOG_BUFFER 4096 +static void vlogf(int lvl, const char *fmt, va_list ap) +{ + char buffer[1 + LOG_BUFFER]; + char *lvl_str, *t; + FILE *stdf; + + if (lvl > LOG_INFO && (!file || lvl > log_level)) + return ; + + vsnprintf(buffer, LOG_BUFFER, fmt, ap); + buffer[LOG_BUFFER] = '\0'; + + switch (lvl) { + case LOG_WARN: + lvl_str = "[WARN]"; + break; + case LOG_ERR: + lvl_str = "[ERR]"; + break; + case LOG_DEBUG: + lvl_str = "[DEBUG]"; + break; + case LOG_INFO: + lvl_str = "[INFO]"; + break; + default: + lvl_str = "[??]"; + } + + t = get_current_time_str(); + if (!t) + return ; + + if (file && lvl <= log_level) { + fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer); + fflush(file); + } else { + t = NULL; + } + + if (lvl <= LOG_INFO) { + if (lvl == LOG_WARN || lvl == LOG_ERR) + stdf = stderr; + else + stdf = stdout; + + + fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer); + } + + free(t); +} + +void log_printf(int lvl, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(lvl, fmt, ap); + va_end(ap); +} + +void log_debug(const char *fmt, ...) +{ + va_list ap; + + if (log_level < LOG_DEBUG) + return ; + + va_start(ap, fmt); + vlogf(LOG_DEBUG, fmt, ap); + va_end(ap); +} + +void log_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_ERR, fmt, ap); + va_end(ap); +} + +void log_warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_WARN, fmt, ap); + va_end(ap); +} + +void log_info(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_INFO, fmt, ap); + va_end(ap); +} diff --git a/src/plog.h b/src/plog.h new file mode 100644 index 0000000..185af1a --- /dev/null +++ b/src/plog.h @@ -0,0 +1,44 @@ +/* + Copyright (C) 2010-2014 jeanfi@gmail.com + + 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 _P_LOG_H_ +#define _P_LOG_H_ + +#define P_LOG_VER 1 + +enum log_level { + LOG_ERR , + LOG_WARN, + LOG_INFO, + LOG_DEBUG +}; + +void log_open(const char *path); + +void log_printf(int lvl, const char *fmt, ...); +void log_debug(const char *fmt, ...); +void log_err(const char *fmt, ...); +void log_info(const char *fmt, ...); +void log_warn(const char *fmt, ...); + +void log_close(); + +/* level of the log file. */ +extern int log_level; + +#endif diff --git a/src/ppastats.c b/src/ppastats.c index ed6efe4..7d67bc4 100644 --- a/src/ppastats.c +++ b/src/ppastats.c @@ -25,8 +25,8 @@ #include #include "list.h" -#include "log.h" #include "lp_ws.h" +#include #include "ppastats.h" static struct package_stats *get_package_stats(struct ppa_stats *stats, diff --git a/src/ptime.c b/src/ptime.c new file mode 100644 index 0000000..3eb966b --- /dev/null +++ b/src/ptime.c @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010-2014 jeanfi@gmail.com + * + * 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 + */ +#include +#include + +#include + +static int ISO8601_TIME_LENGTH = 219; /* YYYY-MM-DDThh:mm:ss */ + +char *time_to_str(time_t *t) +{ + struct tm lt; + char *str; + + memset(<, 0, sizeof(struct tm)); + if (!localtime_r(t, <)) + return NULL; + + str = malloc(ISO8601_TIME_LENGTH); + + if (strftime(str, ISO8601_TIME_LENGTH, "%FT%T", <)) { + return str; + } else { + free(str); + return NULL; + } +} + +char *get_current_time_str() +{ + time_t t; + + t = time(NULL); + return time_to_str(&t); +} diff --git a/src/ptime.h b/src/ptime.h new file mode 100644 index 0000000..d560ff3 --- /dev/null +++ b/src/ptime.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2010-2014 jeanfi@gmail.com + * + * 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 _P_TIME_H +#define _P_TIME_H + +#define P_TIME_VER 1 + +#include + +char *get_current_time_str(); +char *time_to_str(time_t *t); + +#endif -- 2.7.4