added module to handle log
authorJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 6 May 2012 11:04:48 +0000 (11:04 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Sun, 6 May 2012 11:04:48 +0000 (11:04 +0000)
fixed some strings

15 files changed:
po/Makefile
po/POTFILES
po/POTFILES.in
po/fr.po
po/ppastats.pot
src/Makefile.am
src/Makefile.in
src/cache.c
src/html.c
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]
src/lp_ws.c
src/main.c
src/ppastats.c
src/ppastats.h

index abc8a96..97f7a5c 100644 (file)
@@ -73,7 +73,12 @@ $(POFILES) $(GMOFILES) \
 $(DISTFILES.extra1) $(DISTFILES.extra2) $(DISTFILES.extra3)
 
 POTFILES = \
 $(DISTFILES.extra1) $(DISTFILES.extra2) $(DISTFILES.extra3)
 
 POTFILES = \
-     ../src/main.c
+     ../src/cache.c \
+     ../src/html.c \
+     ../src/log.c \
+     ../src/lp_ws.c \
+     ../src/main.c \
+     ../src/ppastats.c
 
 CATALOGS =  fr.gmo
 
 
 CATALOGS =  fr.gmo
 
index 403b79c..d3900b9 100644 (file)
@@ -1 +1,6 @@
-     ../src/main.c
+     ../src/cache.c \
+     ../src/html.c \
+     ../src/log.c \
+     ../src/lp_ws.c \
+     ../src/main.c \
+     ../src/ppastats.c
index 1221e82..0e2c8c9 100644 (file)
@@ -1,2 +1,7 @@
 # List of source files which contain translatable strings.
 # List of source files which contain translatable strings.
+src/cache.c
+src/html.c
+src/log.c
+src/lp_ws.c
 src/main.c
 src/main.c
+src/ppastats.c
index 0795101..6b1efaf 100644 (file)
--- a/po/fr.po
+++ b/po/fr.po
@@ -7,7 +7,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: ppastats 0.0.x\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
 msgstr ""
 "Project-Id-Version: ppastats 0.0.x\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
-"POT-Creation-Date: 2012-05-05 23:52+0200\n"
+"POT-Creation-Date: 2012-05-06 13:01+0200\n"
 "PO-Revision-Date: 2012-05-05 23:45+0200\n"
 "Last-Translator:  <jeanfi@gmail.com>\n"
 "Language-Team: French\n"
 "PO-Revision-Date: 2012-05-05 23:45+0200\n"
 "Last-Translator:  <jeanfi@gmail.com>\n"
 "Language-Team: French\n"
@@ -17,7 +17,68 @@ msgstr ""
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
 "Content-Transfer-Encoding: 8bit\n"
 "Plural-Forms: nplurals=2; plural=(n > 1);\n"
 
-#: src/main.c:101
+#: src/cache.c:57
+#, c-format
+msgid "cache hit %s"
+msgstr ""
+
+#: src/cache.c:62
+#, c-format
+msgid "cache miss %s"
+msgstr ""
+
+#: src/cache.c:71
+msgid "exceed cache capacity"
+msgstr ""
+
+#: src/html.c:392 src/html.c:417
+#, c-format
+msgid "failed to open: %s"
+msgstr ""
+
+#: src/html.c:448 src/html.c:468 src/html.c:481
+#, c-format
+msgid "generating %s"
+msgstr ""
+
+#: src/html.c:544
+#, c-format
+msgid "copying %s %s"
+msgstr ""
+
+#: src/log.c:37
+#, c-format
+msgid "Cannot open log file: %s"
+msgstr ""
+
+#: src/lp_ws.c:71
+#, c-format
+msgid "fetch_url(): %s"
+msgstr ""
+
+#: src/lp_ws.c:74
+msgid "initializing CURL"
+msgstr ""
+
+#: src/lp_ws.c:109
+#, c-format
+msgid "Fetch failed with code %ld for URL %s"
+msgstr ""
+
+#: src/lp_ws.c:114
+msgid "Wait 5s before retry"
+msgstr ""
+
+#: src/lp_ws.c:122
+#, c-format
+msgid "Fetch failed: %ld"
+msgstr ""
+
+#: src/lp_ws.c:294
+msgid "cleanup CURL"
+msgstr ""
+
+#: src/main.c:102
 #, c-format
 msgid ""
 "Copyright (C) %s jeanfi@gmail.com\n"
 #, c-format
 msgid ""
 "Copyright (C) %s jeanfi@gmail.com\n"
@@ -34,19 +95,73 @@ msgstr ""
 "Ce logiciel n'est accompagné d’ABSOLUMENT AUCUNE GARANTIE, dans les limites\n"
 "autorisées par la loi applicable.\n"
 
 "Ce logiciel n'est accompagné d’ABSOLUMENT AUCUNE GARANTIE, dans les limites\n"
 "autorisées par la loi applicable.\n"
 
-#: src/main.c:111
+#: src/main.c:112
 #, c-format
 msgid "Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 msgstr "Utilisation: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 
 #, c-format
 msgid "Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 msgstr "Utilisation: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 
-#: src/main.c:120
+#: src/main.c:114
+msgid "ppastats is a command application for generating PPA statistics.\n"
+msgstr ""
+
+#: src/main.c:117
+msgid ""
+"Prints number of downloads for each published packages of a PPA or generates "
+"an HTML page containing a graph representation."
+msgstr ""
+
+#: src/main.c:122
 msgid "Options:"
 msgstr "Options:"
 
 msgid "Options:"
 msgstr "Options:"
 
-#: src/main.c:121
+#: src/main.c:123
 msgid ""
 "  -h, --help          display this help and exit\n"
 "  -v, --version       display version information and exit"
 msgstr ""
 "  -h, --help          afficher cette aide et quitter\n"
 "  -v, --version       afficher les informations de version et quitter"
 msgid ""
 "  -h, --help          display this help and exit\n"
 "  -v, --version       display version information and exit"
 msgstr ""
 "  -h, --help          afficher cette aide et quitter\n"
 "  -v, --version       afficher les informations de version et quitter"
+
+#: src/main.c:129
+msgid "  -o, --output-dir=[PATH]       generates HTML pages into 'PATH'."
+msgstr ""
+
+#: src/main.c:132
+msgid ""
+"  -s, --status=[STATUS]\t\tretrieves only package of the given status\n"
+"\t\t\t\t(possible values are: Pending, Published, \n"
+"\t\t\t\tSuperseded, Deleted or Obsolete)."
+msgstr ""
+
+#: src/main.c:137
+msgid "  -S, --skip-js-css\t\tskip installation of js and css files"
+msgstr ""
+
+#: src/main.c:141
+#, c-format
+msgid "Report bugs to: %s\n"
+msgstr ""
+
+#: src/main.c:143
+#, c-format
+msgid "%s home page: <%s>\n"
+msgstr ""
+
+#: src/main.c:193
+#, c-format
+msgid "Try `%s --help' for more information.\n"
+msgstr ""
+
+#: src/ppastats.c:254
+msgid "Failed to retrieve PPA information"
+msgstr ""
+
+#: src/ppastats.c:269
+#, c-format
+msgid "Failed to retrieve download totals for %s"
+msgstr ""
+
+#: src/ppastats.c:275
+#, c-format
+msgid "Failed to retrieve download count for %s"
+msgstr ""
index 12dd2c0..e151df2 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: ppastats 0.0.x\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
 msgstr ""
 "Project-Id-Version: ppastats 0.0.x\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
-"POT-Creation-Date: 2012-05-05 23:52+0200\n"
+"POT-Creation-Date: 2012-05-06 13:01+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,7 +17,68 @@ msgstr ""
 "Content-Type: text/plain; charset=CHARSET\n"
 "Content-Transfer-Encoding: 8bit\n"
 
 "Content-Type: text/plain; charset=CHARSET\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: src/main.c:101
+#: src/cache.c:57
+#, c-format
+msgid "cache hit %s"
+msgstr ""
+
+#: src/cache.c:62
+#, c-format
+msgid "cache miss %s"
+msgstr ""
+
+#: src/cache.c:71
+msgid "exceed cache capacity"
+msgstr ""
+
+#: src/html.c:392 src/html.c:417
+#, c-format
+msgid "failed to open: %s"
+msgstr ""
+
+#: src/html.c:448 src/html.c:468 src/html.c:481
+#, c-format
+msgid "generating %s"
+msgstr ""
+
+#: src/html.c:544
+#, c-format
+msgid "copying %s %s"
+msgstr ""
+
+#: src/log.c:37
+#, c-format
+msgid "Cannot open log file: %s"
+msgstr ""
+
+#: src/lp_ws.c:71
+#, c-format
+msgid "fetch_url(): %s"
+msgstr ""
+
+#: src/lp_ws.c:74
+msgid "initializing CURL"
+msgstr ""
+
+#: src/lp_ws.c:109
+#, c-format
+msgid "Fetch failed with code %ld for URL %s"
+msgstr ""
+
+#: src/lp_ws.c:114
+msgid "Wait 5s before retry"
+msgstr ""
+
+#: src/lp_ws.c:122
+#, c-format
+msgid "Fetch failed: %ld"
+msgstr ""
+
+#: src/lp_ws.c:294
+msgid "cleanup CURL"
+msgstr ""
+
+#: src/main.c:102
 #, c-format
 msgid ""
 "Copyright (C) %s jeanfi@gmail.com\n"
 #, c-format
 msgid ""
 "Copyright (C) %s jeanfi@gmail.com\n"
@@ -27,17 +88,71 @@ msgid ""
 "There is NO WARRANTY, to the extent permitted by law.\n"
 msgstr ""
 
 "There is NO WARRANTY, to the extent permitted by law.\n"
 msgstr ""
 
-#: src/main.c:111
+#: src/main.c:112
 #, c-format
 msgid "Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 msgstr ""
 
 #, c-format
 msgid "Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"
 msgstr ""
 
-#: src/main.c:120
+#: src/main.c:114
+msgid "ppastats is a command application for generating PPA statistics.\n"
+msgstr ""
+
+#: src/main.c:117
+msgid ""
+"Prints number of downloads for each published packages of a PPA or generates "
+"an HTML page containing a graph representation."
+msgstr ""
+
+#: src/main.c:122
 msgid "Options:"
 msgstr ""
 
 msgid "Options:"
 msgstr ""
 
-#: src/main.c:121
+#: src/main.c:123
 msgid ""
 "  -h, --help          display this help and exit\n"
 "  -v, --version       display version information and exit"
 msgstr ""
 msgid ""
 "  -h, --help          display this help and exit\n"
 "  -v, --version       display version information and exit"
 msgstr ""
+
+#: src/main.c:129
+msgid "  -o, --output-dir=[PATH]       generates HTML pages into 'PATH'."
+msgstr ""
+
+#: src/main.c:132
+msgid ""
+"  -s, --status=[STATUS]\t\tretrieves only package of the given status\n"
+"\t\t\t\t(possible values are: Pending, Published, \n"
+"\t\t\t\tSuperseded, Deleted or Obsolete)."
+msgstr ""
+
+#: src/main.c:137
+msgid "  -S, --skip-js-css\t\tskip installation of js and css files"
+msgstr ""
+
+#: src/main.c:141
+#, c-format
+msgid "Report bugs to: %s\n"
+msgstr ""
+
+#: src/main.c:143
+#, c-format
+msgid "%s home page: <%s>\n"
+msgstr ""
+
+#: src/main.c:193
+#, c-format
+msgid "Try `%s --help' for more information.\n"
+msgstr ""
+
+#: src/ppastats.c:254
+msgid "Failed to retrieve PPA information"
+msgstr ""
+
+#: src/ppastats.c:269
+#, c-format
+msgid "Failed to retrieve download totals for %s"
+msgstr ""
+
+#: src/ppastats.c:275
+#, c-format
+msgid "Failed to retrieve download count for %s"
+msgstr ""
index e19e20a..03bb962 100644 (file)
@@ -13,6 +13,7 @@ ppastats_SOURCES = \
        html.h html.c\
        io.h io.c\
        list.h list.c\
        html.h html.c\
        io.h io.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\
        lp.h lp.c\
        lp_json.h lp_json.c\
        lp_ws.h lp_ws.c\
index a8341ef..3dfb16b 100644 (file)
@@ -54,8 +54,8 @@ CONFIG_CLEAN_VPATH_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
 PROGRAMS = $(bin_PROGRAMS)
 am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) io.$(OBJEXT) \
 am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
 PROGRAMS = $(bin_PROGRAMS)
 am_ppastats_OBJECTS = cache.$(OBJEXT) html.$(OBJEXT) io.$(OBJEXT) \
-       list.$(OBJEXT) lp.$(OBJEXT) lp_json.$(OBJEXT) lp_ws.$(OBJEXT) \
-       main.$(OBJEXT) ppastats.$(OBJEXT)
+       list.$(OBJEXT) log.$(OBJEXT) lp.$(OBJEXT) lp_json.$(OBJEXT) \
+       lp_ws.$(OBJEXT) main.$(OBJEXT) ppastats.$(OBJEXT)
 ppastats_OBJECTS = $(am_ppastats_OBJECTS)
 ppastats_LDADD = $(LDADD)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
 ppastats_OBJECTS = $(am_ppastats_OBJECTS)
 ppastats_LDADD = $(LDADD)
 DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)
@@ -231,6 +231,7 @@ ppastats_SOURCES = \
        html.h html.c\
        io.h io.c\
        list.h list.c\
        html.h html.c\
        io.h io.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\
        lp.h lp.c\
        lp_json.h lp_json.c\
        lp_ws.h lp_ws.c\
@@ -323,6 +324,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/io.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/list.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/html.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/io.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)/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@
index 940294e..cd24367 100644 (file)
     02110-1301 USA
 */
 
     02110-1301 USA
 */
 
+#include <libintl.h>
+#define _(String) gettext(String)
+
 #include <string.h>
 
 #include "cache.h"
 #include <string.h>
 
 #include "cache.h"
+#include "log.h"
 #include "ppastats.h"
 
 #include <stdlib.h>
 #include "ppastats.h"
 
 #include <stdlib.h>
@@ -50,14 +54,12 @@ const void *cache_get(const char *key)
 
        for (i = 0; i < cache.size; i++)
                if (!strcmp(cache.entries[i].key, key)) {
 
        for (i = 0; i < cache.size; i++)
                if (!strcmp(cache.entries[i].key, key)) {
-                       if (debug)
-                               printf("DEBUG: cache hit %s\n", key);
+                       log_debug(_("cache hit %s"), key);
 
                        return cache.entries[i].value;
                }
 
 
                        return cache.entries[i].value;
                }
 
-       if (debug)
-               printf("DEBUG: cache miss %s\n", key);
+       log_debug(_("cache miss %s"), key);
 
        return NULL;
 }
 
        return NULL;
 }
@@ -66,7 +68,7 @@ void cache_put(const char *key, const void *value,
               void (*fct_cleanup)(void *))
 {
        if (cache.size == CAPACITY) {
               void (*fct_cleanup)(void *))
 {
        if (cache.size == CAPACITY) {
-               fprintf(stderr, "WARNING: exceed cache capacity\n");
+               log_warn(_("exceed cache capacity"));
                return ;
        }
 
                return ;
        }
 
index 2fd1eef..e79c2a4 100644 (file)
@@ -17,6 +17,9 @@
     02110-1301 USA
 */
 
     02110-1301 USA
 */
 
+#include <libintl.h>
+#define _(String) gettext(String)
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -28,6 +31,7 @@
 
 #include "html.h"
 #include "io.h"
 
 #include "html.h"
 #include "io.h"
+#include "log.h"
 #include "lp.h"
 #include "lp_ws.h"
 #include "ppastats.h"
 #include "lp.h"
 #include "lp_ws.h"
 #include "ppastats.h"
@@ -385,7 +389,7 @@ version_to_html(struct ppa_stats *ppa,
        f = fopen(path, "w");
 
        if (!f) {
        f = fopen(path, "w");
 
        if (!f) {
-               fprintf(stderr, "ERROR: failed to open: %s\n", path);
+               log_err(_("failed to open: %s"), path);
                return ;
        }
 
                return ;
        }
 
@@ -410,7 +414,7 @@ create_html(const char *path,
        f = fopen(path, "w");
 
        if (!f) {
        f = fopen(path, "w");
 
        if (!f) {
-               fprintf(stderr, "ERROR: failed to open: %s\n", path);
+               log_err(_("failed to open: %s"), path);
                return ;
        }
 
                return ;
        }
 
@@ -440,8 +444,8 @@ index_to_html(struct ppa_stats *ppa, const char *dir)
 
        json = ppa_to_json(ppa);
        json_path = path_new(dir, "index", ".json");
 
        json = ppa_to_json(ppa);
        json_path = path_new(dir, "index", ".json");
-       if (debug)
-               printf("DEBUG: generating %s\n", json_path);
+
+       log_debug(_("generating %s"), json_path);
        json_object_to_file(json_path, json);
        json_object_put(json);
        free(json_path);
        json_object_to_file(json_path, json);
        json_object_put(json);
        free(json_path);
@@ -461,8 +465,8 @@ pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
 
        json_path = path_new(dir, pkg->name, ".json");
        json = pkg_to_json(ppa, pkg);
 
        json_path = path_new(dir, pkg->name, ".json");
        json = pkg_to_json(ppa, pkg);
-       if (debug)
-               printf("DEBUG: generating %s\n", json_path);
+       log_debug(_("generating %s"), json_path);
+
        json_object_to_file(json_path, json);
        json_object_put(json);
        free(json_path);
        json_object_to_file(json_path, json);
        json_object_put(json);
        free(json_path);
@@ -474,8 +478,7 @@ pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
                        1);
        sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
 
                        1);
        sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
 
-       if (debug)
-               printf("DEBUG: generating %s\n", path);
+       log_debug(_("generating %s"), path);
 
        create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
        free(path);
 
        create_html(path, pkg->name, HTML_PKG_TEMPLATE, script);
        free(path);
@@ -538,9 +541,7 @@ ppa_to_html(const char *owner,
                for (i = 0; i < 7; i++) {
                        f_dst = path_append(output_dir, www_files[2*i+1]);
 
                for (i = 0; i < 7; i++) {
                        f_dst = path_append(output_dir, www_files[2*i+1]);
 
-                       if (debug)
-                               printf("DEBUG: copying %s %s\n",
-                                      www_files[2*i], f_dst);
+                       log_debug(_("copying %s %s"), www_files[2*i], f_dst);
                        fcopy(www_files[2*i], f_dst);
 
                        free(f_dst);
                        fcopy(www_files[2*i], f_dst);
 
                        free(f_dst);
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..0790345
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2010-2012 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);
+}
+
+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
new file mode 100644 (file)
index 0000000..e3d64fd
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2010-2012 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 5eca19a..81b8b63 100644 (file)
@@ -16,6 +16,8 @@
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     02110-1301 USA
 */
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     02110-1301 USA
 */
+#include <libintl.h>
+#define _(String) gettext(String)
 
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
@@ -26,6 +28,7 @@
 
 #include "cache.h"
 #include "list.h"
 
 #include "cache.h"
 #include "list.h"
+#include "log.h"
 #include "lp_ws.h"
 #include "lp_json.h"
 #include "ppastats.h"
 #include "lp_ws.h"
 #include "lp_json.h"
 #include "ppastats.h"
@@ -65,12 +68,10 @@ static char *fetch_url(const char *url)
        long code;
        int retries;
 
        long code;
        int retries;
 
-       if (debug)
-               printf("DEBUG: fetch_url %s\n", url);
+       log_debug(_("fetch_url(): %s"), url);
 
        if (!curl) {
 
        if (!curl) {
-               if (debug)
-                       printf("DEBUG: initializing CURL\n");
+               log_debug(_("initializing CURL"));
                curl_global_init(CURL_GLOBAL_ALL);
                curl = curl_easy_init();
        }
                curl_global_init(CURL_GLOBAL_ALL);
                curl = curl_easy_init();
        }
@@ -105,14 +106,12 @@ static char *fetch_url(const char *url)
                case 503:
                case 504:
                        if (retries) {
                case 503:
                case 504:
                        if (retries) {
-                               fprintf(stderr,
-                                       "Fetch failed: with code %ld "
-                                       "for URL= %s\n",
+                               log_err(_("Fetch failed with code %ld "
+                                         "for URL %s"),
                                        code,
                                        url);
 
                                        code,
                                        url);
 
-                               if (debug)
-                                       printf("Wait 5s before retry.\n");
+                               log_debug(_("Wait 5s before retry"));
                                sleep(5);
 
                                free(content->data);
                                sleep(5);
 
                                free(content->data);
@@ -120,7 +119,7 @@ static char *fetch_url(const char *url)
                                goto retrieve;
                        }
                default:
                                goto retrieve;
                        }
                default:
-                       fprintf(stderr, "Fetch failed: %ld\n", code);
+                       log_err(_("Fetch failed: %ld"), code);
                }
        }
 
                }
        }
 
@@ -292,8 +291,7 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url)
 
 void lp_ws_cleanup()
 {
 
 void lp_ws_cleanup()
 {
-       if (debug)
-               printf("DEBUG: cleanup CURL\n");
+       log_debug(_("cleanup CURL"));
 
        curl_easy_cleanup(curl);
        curl_global_cleanup();
 
        curl_easy_cleanup(curl);
        curl_global_cleanup();
index 94f6f8d..4f3a227 100644 (file)
 
 #include "cache.h"
 #include "html.h"
 
 #include "cache.h"
 #include "html.h"
+#include "log.h"
 #include "lp_ws.h"
 #include "config.h"
 #include "ppastats.h"
 
 #include "lp_ws.h"
 #include "config.h"
 #include "ppastats.h"
 
-int debug;
 static const char *program_name;
 
 static void display_published_binaries(const char *owner,
 static const char *program_name;
 
 static void display_published_binaries(const char *owner,
@@ -168,7 +168,7 @@ int main(int argc, char **argv)
                        output_dir = strdup(optarg);
                        break;
                case 'd':
                        output_dir = strdup(optarg);
                        break;
                case 'd':
-                       debug = 1;
+                       log_level = LOG_DEBUG;
                        break;
                case 'h':
                        print_help();
                        break;
                case 'h':
                        print_help();
index 38f6dd0..7861301 100644 (file)
     02110-1301 USA
 */
 
     02110-1301 USA
 */
 
+#include <libintl.h>
+#define _(String) gettext(String)
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "list.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "list.h"
+#include "log.h"
 #include "lp_ws.h"
 #include "ppastats.h"
 
 #include "lp_ws.h"
 #include "ppastats.h"
 
@@ -247,7 +251,7 @@ create_ppa_stats(const char *owner,
        free(ppa_url);
 
        if (!history) {
        free(ppa_url);
 
        if (!history) {
-               fprintf(stderr, "Failed to retrieve PPA information\n");
+               log_err(_("Failed to retrieve PPA information"));
                exit(EXIT_FAILURE);
        }
 
                exit(EXIT_FAILURE);
        }
 
@@ -262,14 +266,14 @@ create_ppa_stats(const char *owner,
                h = *h_cur;
                totals = get_daily_download_totals(h->self_link);
                if (!totals) {
                h = *h_cur;
                totals = get_daily_download_totals(h->self_link);
                if (!totals) {
-                       fprintf(stderr, "Failed to retrieve download totals"
-                               " for %s\n", h->self_link);
+                       log_err(_("Failed to retrieve download totals for %s"),
+                               h->self_link);
                        continue;
                }
                count = get_download_count(h->self_link);
                if (count == -1) {
                        continue;
                }
                count = get_download_count(h->self_link);
                if (count == -1) {
-                       fprintf(stderr, "Failed to retrieve download count"
-                               " for %s\n", h->self_link);
+                       log_err(_("Failed to retrieve download count for %s"),
+                               h->self_link);
                        count = 0;
                }
                pkg_name = h->binary_package_name;
                        count = 0;
                }
                pkg_name = h->binary_package_name;
index 4fe6c65..b6b12df 100644 (file)
@@ -20,8 +20,6 @@
 #ifndef _PPASTATS_PPASTATS_H_
 #define _PPASTATS_PPASTATS_H_
 
 #ifndef _PPASTATS_PPASTATS_H_
 #define _PPASTATS_PPASTATS_H_
 
-extern int debug;
-
 #include "lp.h"
 
 struct arch_stats {
 #include "lp.h"
 
 struct arch_stats {