merged plib
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sat, 15 Feb 2014 21:42:29 +0000 (21:42 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sat, 15 Feb 2014 21:42:29 +0000 (21:42 +0000)
18 files changed:
src/Makefile.am
src/Makefile.in
src/cache.c
src/fcache.c
src/html.c
src/http.c
src/log.c [deleted file]
src/log.h [deleted file]
src/lp.c
src/lp.h
src/lp_json.c
src/lp_ws.c
src/main.c
src/plog.c [new file with mode: 0644]
src/plog.h [new file with mode: 0644]
src/ppastats.c
src/ptime.c [new file with mode: 0644]
src/ptime.h [new file with mode: 0644]

index 0173371..96f0c4e 100644 (file)
@@ -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
 
index fb6c621..af1af11 100644 (file)
@@ -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 $@ $<
index 8053421..a25c47d 100644 (file)
@@ -25,7 +25,7 @@
 #include <string.h>
 
 #include "cache.h"
-#include "log.h"
+#include <plog.h>
 #include "ppastats.h"
 
 
index 852d977..1c9fe66 100644 (file)
@@ -26,7 +26,7 @@
 #include <string.h>
 
 #include "fcache.h"
-#include "log.h"
+#include <plog.h>
 #include <pio.h>
 
 static const char *cache_dir;
index 379a9c1..408c18e 100644 (file)
 #include <json.h>
 
 #include "html.h"
-#include "log.h"
 #include "lp.h"
 #include <lp_json.h>
 #include "lp_ws.h"
 #include "ppastats.h"
 #include <pio.h>
+#include <plog.h>
 #include <pstr.h>
 
 static const char *footer;
index d6c7983..4370f25 100644 (file)
@@ -28,7 +28,7 @@
 
 #include "fcache.h"
 #include "http.h"
-#include "log.h"
+#include <plog.h>
 
 static const int DEFAULT_FETCH_RETRIES = 10;
 
diff --git a/src/log.c b/src/log.c
deleted file mode 100644 (file)
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 <locale.h>
-#include <libintl.h>
-#define _(str) gettext(str)
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <sys/time.h>
-
-#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 (file)
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
index e23b77b..e738a6c 100644 (file)
--- a/src/lp.c
+++ b/src/lp.c
 
 #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 <ptime.h>
 
 struct distro_series *distro_series_new(const char *name,
                                        const char *version,
index 394aa17..47c861f 100644 (file)
--- 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 **
index c3345fd..0643a1b 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "lp_json.h"
 #include "lp_ws.h"
+#include <ptime.h>
 
 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);
index 8e66167..0dce140 100644 (file)
 #include "fcache.h"
 #include "http.h"
 #include "list.h"
-#include "log.h"
 #include "lp_ws.h"
 #include "lp_json.h"
+#include <plog.h>
 #include "ppastats.h"
+#include <ptime.h>
 
 /** 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;
 }
index e1f17d5..58cb028 100644 (file)
@@ -28,9 +28,9 @@
 #include "cache.h"
 #include "html.h"
 #include "http.h"
-#include "log.h"
 #include "lp_ws.h"
 #include "config.h"
+#include <plog.h>
 #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 (file)
index 0000000..be6d09e
--- /dev/null
@@ -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 <locale.h>
+#include <libintl.h>
+#define _(str) gettext(str)
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include <plog.h>
+#include <ptime.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)
+{
+       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 (file)
index 0000000..185af1a
--- /dev/null
@@ -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
index ed6efe4..7d67bc4 100644 (file)
@@ -25,8 +25,8 @@
 #include <string.h>
 
 #include "list.h"
-#include "log.h"
 #include "lp_ws.h"
+#include <plog.h>
 #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 (file)
index 0000000..3eb966b
--- /dev/null
@@ -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 <stdlib.h>
+#include <string.h>
+
+#include <ptime.h>
+
+static int ISO8601_TIME_LENGTH = 219;  /* YYYY-MM-DDThh:mm:ss */
+
+char *time_to_str(time_t *t)
+{
+       struct tm lt;
+       char *str;
+
+       memset(&lt, 0, sizeof(struct tm));
+       if (!localtime_r(t, &lt))
+               return NULL;
+
+       str = malloc(ISO8601_TIME_LENGTH);
+
+       if (strftime(str, ISO8601_TIME_LENGTH, "%FT%T", &lt)) {
+               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 (file)
index 0000000..d560ff3
--- /dev/null
@@ -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 <time.h>
+
+char *get_current_time_str();
+char *time_to_str(time_t *t);
+
+#endif