(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Thu, 25 Apr 2013 19:35:37 +0000 (19:35 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Thu, 25 Apr 2013 19:35:37 +0000 (19:35 +0000)
18 files changed:
po/Makefile
po/POTFILES
po/POTFILES.in
po/fr.gmo
po/fr.po
po/prss.pot
src/Makefile.am
src/Makefile.in
src/glade/prss.glade
src/http.c
src/log.c [new file with mode: 0644]
src/log.h [new file with mode: 0644]
src/main.c
src/ttrss.c
src/ttrss.h
src/ttrss_model.c
src/ttrss_ws.c
src/ttrss_ws.h

index 1aa077a..6b758d1 100644 (file)
@@ -72,6 +72,7 @@ $(POFILES) $(GMOFILES) \
 $(DISTFILES.extra1) $(DISTFILES.extra2) $(DISTFILES.extra3)
 
 POTFILES = \
+     ../src/log.c \
      ../src/main.c
 
 CATALOGS =  fr.gmo
index 403b79c..bdeb3c3 100644 (file)
@@ -1 +1,2 @@
+     ../src/log.c \
      ../src/main.c
index e723152..d11d321 100644 (file)
@@ -1 +1,2 @@
+src/log.c
 src/main.c
index 6c59182..d0e187a 100644 (file)
Binary files a/po/fr.gmo and b/po/fr.gmo differ
index 5582f5e..a3e3ef3 100644 (file)
--- a/po/fr.po
+++ b/po/fr.po
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: ptask\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
-"POT-Creation-Date: 2013-04-25 09:29+0200\n"
+"POT-Creation-Date: 2013-04-25 15:45+0200\n"
 "PO-Revision-Date: 2012-05-13 18:15+0000\n"
 "Last-Translator: jeanfi <Unknown>\n"
 "Language-Team: French <>\n"
@@ -19,6 +19,11 @@ msgstr ""
 "X-Launchpad-Export-Date: 2012-07-02 08:28+0000\n"
 "X-Generator: Launchpad (build 15520)\n"
 
+#: src/log.c:37
+#, c-format
+msgid "Cannot open log file: %s"
+msgstr ""
+
 #: src/main.c:57
 #, c-format
 msgid ""
@@ -59,7 +64,7 @@ msgstr ""
 msgid "%s home page: <%s>\n"
 msgstr ""
 
-#: src/main.c:391
+#: src/main.c:393
 #, c-format
 msgid "Try `%s --help' for more information.\n"
 msgstr ""
index 0ff205a..e7165db 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: jeanfi@gmail.com\n"
-"POT-Creation-Date: 2013-04-25 09:29+0200\n"
+"POT-Creation-Date: 2013-04-25 15:45+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"
@@ -17,6 +17,11 @@ msgstr ""
 "Content-Type: text/plain; charset=CHARSET\n"
 "Content-Transfer-Encoding: 8bit\n"
 
+#: src/log.c:37
+#, c-format
+msgid "Cannot open log file: %s"
+msgstr ""
+
 #: src/main.c:57
 #, c-format
 msgid ""
@@ -56,7 +61,7 @@ msgstr ""
 msgid "%s home page: <%s>\n"
 msgstr ""
 
-#: src/main.c:391
+#: src/main.c:393
 #, c-format
 msgid "Try `%s --help' for more information.\n"
 msgstr ""
index 4a5d367..348772c 100644 (file)
@@ -16,6 +16,8 @@ prss_SOURCES = main.c \
        http.h \
        io.c \
        io.h \
+       log.c \
+       log.h \
        ttrss.c \
        ttrss.h \
        ttrss_model.c \
index ed14e3a..47d4df3 100644 (file)
@@ -65,8 +65,8 @@ CONFIG_CLEAN_VPATH_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)"
 PROGRAMS = $(bin_PROGRAMS)
 am_prss_OBJECTS = main.$(OBJEXT) http.$(OBJEXT) io.$(OBJEXT) \
-       ttrss.$(OBJEXT) ttrss_model.$(OBJEXT) ttrss_ws.$(OBJEXT) \
-       url.$(OBJEXT) webbrowser.$(OBJEXT)
+       log.$(OBJEXT) ttrss.$(OBJEXT) ttrss_model.$(OBJEXT) \
+       ttrss_ws.$(OBJEXT) url.$(OBJEXT) webbrowser.$(OBJEXT)
 prss_OBJECTS = $(am_prss_OBJECTS)
 prss_LDADD = $(LDADD)
 DEFAULT_INCLUDES = -I.@am__isrc@
@@ -297,6 +297,8 @@ prss_SOURCES = main.c \
        http.h \
        io.c \
        io.h \
+       log.c \
+       log.h \
        ttrss.c \
        ttrss.h \
        ttrss_model.c \
@@ -397,6 +399,7 @@ distclean-compile:
 
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/http.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/io.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_model.Po@am__quote@
index e3f14e8..d1de23f 100644 (file)
               <object class="GtkEntry" id="password_entry">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
+                <property name="visibility">False</property>
                 <property name="invisible_char">•</property>
               </object>
               <packing>
index 43053ed..52238ec 100644 (file)
@@ -27,8 +27,7 @@
 #include <curl/curl.h>
 
 #include "http.h"
-
-static int debug;
+#include "log.h"
 
 struct ucontent {
        char *data;
@@ -87,7 +86,7 @@ char *http_get(const char *url, const char *content)
                return chunk.data;
 
        free(chunk.data);
-       fprintf(stderr, _("HTTP request fail url=%s"), url);
+       log_err(_("HTTP request fail url=%s"), url);
 
        return NULL;
 }
@@ -98,17 +97,19 @@ json_object *http_json_get(const char *url, struct json_object *j)
        char *out;
        struct json_object *result;
 
+       if (log_level >= LOG_DEBUG)
+               log_debug("HTTP request= %s",
+                      json_object_to_json_string(j));
+
        in = json_object_to_json_string(j);
        out = http_get(url, in);
 
-       if (debug)
-               printf("HTTP request= %s\n",
-                      json_object_to_json_string(j));
-
        if (out) {
                result = json_tokener_parse(out);
-               if (debug)
-                       printf("HTTP reply= %s\n", out);
+
+               if (log_level >= LOG_DEBUG)
+                       log_debug("HTTP reply= %s", out);
+
                free(out);
                return result;
        }
diff --git a/src/log.c b/src/log.c
new file mode 100644 (file)
index 0000000..3009e5d
--- /dev/null
+++ b/src/log.c
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2010-2013 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 128
+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_INFO && (!file || 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 && lvl <= log_level) {
+               fprintf(file,
+                       "[%ld.%06ld] %s %s\n",
+                       tv.tv_sec,
+                       tv.tv_usec,
+                       lvl_str, 
+                       buffer);
+               fflush(file);
+       }
+
+       if (lvl <= LOG_INFO) {
+               if (lvl == LOG_WARN || lvl == LOG_ERR)
+                       stdf = stderr;
+               else
+                       stdf = stdout;
+
+               fprintf(stdf,
+                       "[%ld.%06ld] %s %s\n",
+                       tv.tv_sec,
+                       tv.tv_usec,
+                       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);
+}
+
+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/log.h b/src/log.h
new file mode 100644 (file)
index 0000000..aebaa07
--- /dev/null
+++ b/src/log.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2010-2013 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 _PRSS_LOG_H_
+#define _PRSS_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_info(const char *fmt, ...);
+void log_warn(const char *fmt, ...);
+
+void log_close();
+
+/* level of the log file. */
+extern int log_level;
+
+#endif
index 5b23720..be1bd04 100644 (file)
 #include <string.h>
 #include <getopt.h>
 
+#include <sys/stat.h>
+#include <sys/types.h>
+
 #include <json/json.h>
 #include <webkit/webkit.h>
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
 #include "http.h"
+#include "log.h"
 #include "ttrss.h"
+#include "ttrss_ws.h"
 #include "webbrowser.h"
 
 static const char *program_name;
@@ -48,6 +53,7 @@ enum {
 static struct option long_options[] = {
        {"version", no_argument, 0, 'v'},
        {"help", no_argument, 0, 'h'},
+       {"debug", required_argument, 0, 'd'},
        {0, 0, 0, 0}
 };
 
@@ -73,6 +79,9 @@ static void print_help()
        puts(_("Options:"));
        puts(_("  -h, --help          display this help and exit\n"
               "  -v, --version       display version information and exit"));
+       puts("");
+       puts(_("  -d, --debug=LEVEL   "
+              "set the debug level, integer between 0 and 3"));
 
        puts("");
 
@@ -93,12 +102,12 @@ void update()
 
        model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_feedtree));
 
-       printf("update(): clear feed tree\n");
+       log_debug("update(): clear feed tree.");
        model_state = 1;
        gtk_list_store_clear(GTK_LIST_STORE(model));
        headlinemodel = gtk_tree_view_get_model(w_headlinetree);
        gtk_list_store_clear(GTK_LIST_STORE(headlinemodel));
-       printf("update(): clear feed tree done\n");
+       log_debug("update(): clear feed tree done.");
        feeds = ttrss_get_feeds();
        while (feeds && *feeds) {
                if ((*feeds)->unread)
@@ -121,13 +130,14 @@ void update()
        }
        model_state = 0;
 
-       printf("refresh done\n");
+       log_debug("update() done");
 }
 
 void refresh_clicked_cbk(GtkWidget *btn, gpointer data)
 {
-       printf("refresh_clicked_cbk\n");
+       log_debug("refresh_clicked_cbk()");
        update();
+       log_debug("refresh_clicked_cbk() done");
 }
 
 void settings_activate_cbk(GtkWidget *menu_item, gpointer data)
@@ -136,7 +146,7 @@ void settings_activate_cbk(GtkWidget *menu_item, gpointer data)
        GtkBuilder *builder;
        GtkEntry *login, *url, *pwd;
 
-       printf("settings_activate_cbk\n");
+       log_debug("settings_activate_cbk()");
 
        builder = gtk_builder_new();
 
@@ -167,6 +177,11 @@ void settings_activate_cbk(GtkWidget *menu_item, gpointer data)
                                      "user",
                                      gtk_entry_get_text(login));
 
+               ttrss_set_config(g_settings_get_string(settings, "url"),
+                                g_settings_get_string(settings, "user"),
+                                g_settings_get_string(settings, "password"));
+               ws_open_session();
+
                update();
        }
 
@@ -232,7 +247,7 @@ int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
        if (model_state)
                return TRUE;
 
-       printf("feed_cursor_changed_cbk\n");
+       log_debug("feed_cursor_changed_cbk()");
 
        feed = get_selected_feed();
 
@@ -279,7 +294,7 @@ int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
        struct headline *headline;
        char *str;
 
-       printf("headline_cursor_changed_cbk\n");
+       log_debug("headline_cursor_changed_cbk()");
 
        if (model_state)
                return TRUE;
@@ -307,7 +322,7 @@ int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
                                   0, headline->title,
                                   -1);
        } else {
-               printf("headline not found\n");
+               log_debug("headline not found");
        }
 
        return TRUE;
@@ -324,7 +339,7 @@ void headline_row_activated_cbk(GtkWidget *widget, gpointer data)
        const char *url;
        int id;
 
-       printf("headline_row_activated_cbk\n");
+       printf("headline_row_activated_cbk()");
 
        selection = gtk_tree_view_get_selection(w_headlinetree);
        model = gtk_tree_view_get_model(w_headlinetree);
@@ -351,13 +366,35 @@ void headline_row_activated_cbk(GtkWidget *widget, gpointer data)
 gboolean
 window_delete_event_cbk(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
-       printf("window_delete_event_cbk\n");
+       log_debug("window_delete_event_cbk");
 
        gtk_main_quit();
 
        return TRUE;
 }
 
+static void log_init()
+{
+       char *home, *path, *dir;
+
+       home = getenv("HOME");
+
+       if (!home)
+               return ;
+
+       dir = malloc(strlen(home)+1+strlen(".prss")+1);
+       sprintf(dir, "%s/%s", home, ".prss");
+       mkdir(dir, 0777);
+
+       path = malloc(strlen(dir)+1+strlen("log")+1);
+       sprintf(path, "%s/%s", dir, "log");
+
+       log_open(path);
+
+       free(dir);
+       free(path);
+}
+
 int main(int argc, char **argv)
 {
        GtkWidget *window, *panh;
@@ -374,9 +411,13 @@ int main(int argc, char **argv)
 #endif
 
        cmdok = 1;
-       while ((optc = getopt_long(argc, argv, "vh", long_options,
+       while ((optc = getopt_long(argc, argv, "vhd:", long_options,
                                   &opti)) != -1) {
                switch (optc) {
+               case 'd':
+                       log_level = atoi(optarg);
+                       log_info(_("Enables debug mode."));
+                       break;
                case 'h':
                        print_help();
                        exit(EXIT_SUCCESS);
@@ -395,6 +436,8 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
+       log_init();
+
        http_init();
 
        gtk_init(NULL, NULL);
index 6e32cc0..68b54ff 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "http.h"
 #include "io.h"
+#include "log.h"
 #include "ttrss_ws.h"
 #include "url.h"
 
@@ -68,9 +69,9 @@ char *ttrss_get_headline_content(struct headline *h)
        cache_dir = get_cache_dir();
        if (cache_dir) {
                path = g_strdup_printf("%s/%d", cache_dir, h->id);
-               
+
                content = file_get_content(path);
-               
+
                if (!content) {
                        content = ws_get_article_content(h->id);
                        file_set_content(path, content);
@@ -79,7 +80,7 @@ char *ttrss_get_headline_content(struct headline *h)
                g_free(path);
 
                return content;
-       } 
+       }
 
        return NULL;
 }
@@ -103,7 +104,7 @@ void ttrss_set_article_unread(int id, int unread)
 {
        struct json_object *rp, *rq;
 
-       printf("ttrss_set_article_unread %d %d\n", id, unread);
+       log_debug("ttrss_set_article_unread(%d,%d)", id, unread);
 
        rq = ws_request_new("updateArticle");
        json_object_object_add(rq, "article_ids", json_object_new_int(id));
index dcc68f1..b426a50 100644 (file)
@@ -32,6 +32,5 @@ void ttrss_set_article_unread(int id, int unread);
 struct headline *ttrss_get_headline(int id);
 
 void ttrs_set_config(const char *url, const char *user, const char *pwd);
-int ws_open_session();
 
 #endif
index f08521a..3cba736 100644 (file)
@@ -167,14 +167,10 @@ struct feed *feeds_get_feed(struct feed **feeds, int id)
 {
        struct feed **cur;
 
-       printf("%p\n", feeds);
-
        if (feeds)
                for (cur = feeds; *cur; cur++)
-                       if ((*cur)->id == id) {
-                               printf("match!\n");
+                       if ((*cur)->id == id)
                                return *cur;
-                       }
 
        return NULL;
 }
index cdd7296..f34aaf1 100644 (file)
@@ -24,6 +24,7 @@
 #include <json/json.h>
 
 #include "http.h"
+#include "log.h"
 #include "ttrss_ws.h"
 #include "url.h"
 
@@ -142,14 +143,15 @@ char *ws_login()
        rp = ws_execute(rq);
        json_object_put(rq);
 
+       str = NULL;
        if (rp) {
                j = json_object_object_get(rp, "session_id");
-               str = strdup(json_object_get_string(j));
+
+               if (j)
+                       str = strdup(json_object_get_string(j));
 
                json_object_put(rp);
-       } else {
-               str = NULL;
-       }
+       } 
 
        return str;
 }
@@ -165,7 +167,7 @@ int ws_open_session()
 
        if (session_id) {
                version = ws_get_api_version();
-               printf("API version: %d\n", version);
+               log_debug("API version= %d", version);
 
                if (version > 0) {
                        result = 1;
@@ -266,7 +268,7 @@ struct feed **ws_update_feeds(struct feed **feeds)
        struct feed *feed, **tmp;
        const char *title, *url;
 
-       printf("ttrss_get_feeds\n");
+       log_debug("ttrss_get_feeds()");
 
        rq = ws_request_new("getFeeds");
 
@@ -296,8 +298,6 @@ struct feed **ws_update_feeds(struct feed **feeds)
                                tmp = feeds_add(feeds, feed);
                                free(feeds);
                                feeds = tmp;
-                       } else {
-                               printf("found!\n");
                        }
 
                        j = json_object_object_get(jfeed, "unread");
@@ -310,7 +310,7 @@ struct feed **ws_update_feeds(struct feed **feeds)
 
        json_object_put(rp);
 
-       printf("ttrss_get_feeds ended\n");
+       log_debug("ttrss_get_feeds() done");
 
        return feeds;
 }
index 770b706..7710af4 100644 (file)
@@ -23,6 +23,7 @@
 #include "ttrss.h"
 
 void ws_init(const char *url, const char *user, const char *pwd);
+int ws_open_session();
 
 struct json_object *ws_request_new(const char *op);
 struct json_object *ws_execute(struct json_object *rq);