]> sjero.net Git - linphone/blobdiff - gtk/logging.c
fix various compilation issue
[linphone] / gtk / logging.c
index 4b3af6638303b733840f19ec747a08c8cc04b3fd..26027251f6aad830a5bea120bd2a3fe621ff8536 100644 (file)
@@ -23,11 +23,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <sys/stat.h>
 #include <sys/types.h>
 #endif
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 
+extern gchar *linphone_logfile;
 
 static GtkWidget *log_window=NULL;
 static GStaticMutex log_mutex=G_STATIC_MUTEX_INIT;
 static GList *log_queue=NULL;
+static const char *dateformat="%Y%m%d-%H:%M:%S";
+
+#define LOG_MAX_CHARS 1000000  /*1 mega bytes of traces*/
 
 typedef struct _LinphoneGtkLog{
        OrtpLogLevel lev;
@@ -52,7 +57,7 @@ static FILE *_logfile = NULL;
 
 /* Called on exit, print out the marker, close the file and avoid to
    continue logging. */
-static void linphone_gtk_log_uninit()
+void linphone_gtk_log_uninit()
 {
        if (_logfile != NULL) {
                fprintf(_logfile, "%s\n", LOGFILE_MARKER_STOP);
@@ -70,10 +75,13 @@ static FILE *linphone_gtk_log_init()
        static char _logdir[1024];
        static char _logfname[1024];
        static gboolean _log_init = FALSE;
-       const char *dst_fname;
+       const char *dst_fname=NULL;
 
        if (!_log_init) {
-               dst_fname = linphone_gtk_get_ui_config("logfile",NULL);
+               if (linphone_gtk_get_core()!=NULL){
+                       dst_fname = linphone_gtk_get_ui_config("logfile",NULL);
+                       dateformat=linphone_gtk_get_ui_config("logfile_date_format",dateformat);
+               }
                /* For anything to happen, we need a logfile configuration variable,
                 this is our trigger */
                if (dst_fname) {
@@ -99,51 +107,55 @@ static FILE *linphone_gtk_log_init()
                        }
 #define PATH_SEPARATOR '/'
 #endif
-                       /* We have a directory, fix the path to the log file in it and
-                        open the file so that we will be appending to it. */
                        if (_logdir[0] != '\0') {
-                               snprintf(_logfname, sizeof(_logfname), "%s%c%s",
-                                       _logdir, PATH_SEPARATOR, dst_fname);
-                               /* If the constant LOGFILE_ROTATION is greater than zero, then
-                                we kick away a simple rotation that will ensure that there
-                                are never more than LOGFILE_ROTATION+1 old copies of the
-                                log file on the disk.  The oldest file is always rotated
-                                "away" as expected.  Rotated files have the same name as
-                                the main log file, though with a number 0..LOGFILE_ROTATION
-                                at the end, where the greater the number is, the older the
-                                file is. */
-                               if (ortp_file_exist(_logfname)==0 && LOGFILE_ROTATION > 0) {
-                                       int i;
-                                       char old_fname[1024];
-                                       char new_fname[1024];
-
-                                       /* Rotate away existing files.  We make sure to remove the
-                                        old files otherwise rename() would not work properly.  We
-                                        have to loop in reverse here. */
-                                       for (i=LOGFILE_ROTATION-1;i>=0;i--) {
-                                               snprintf(old_fname, sizeof(old_fname), "%s%c%s.%d",
-                                                       _logdir, PATH_SEPARATOR, dst_fname, i);
-                                               snprintf(new_fname, sizeof(new_fname), "%s%c%s.%d",
-                                                       _logdir, PATH_SEPARATOR, dst_fname, i+1);
-                                               if (ortp_file_exist(old_fname)==0) {
-                                                       if (ortp_file_exist(new_fname)==0)
-                                                               unlink(new_fname);
-                                                       rename(old_fname, new_fname);
-                                               }
-                                       }
-                                       /* Move current log file as the first of the rotation.  Make
-                                        sure to remove the old .0 also, since otherwise rename()
-                                        would not work as expected. */
+                               /* We have a directory, fix the path to the log file in it and
+                                open the file so that we will be appending to it. */
+                               snprintf(_logfname, sizeof(_logfname), "%s%c%s",_logdir, PATH_SEPARATOR, dst_fname);
+                       }
+               }else if (linphone_logfile!=NULL){
+                       snprintf(_logfname,sizeof(_logfname),"%s",linphone_logfile);
+               }
+               
+               if (_logfname[0]!='\0'){
+                       /* If the constant LOGFILE_ROTATION is greater than zero, then
+                        we kick away a simple rotation that will ensure that there
+                        are never more than LOGFILE_ROTATION+1 old copies of the
+                        log file on the disk.  The oldest file is always rotated
+                        "away" as expected.  Rotated files have the same name as
+                        the main log file, though with a number 0..LOGFILE_ROTATION
+                        at the end, where the greater the number is, the older the
+                        file is. */
+                       if (ortp_file_exist(_logfname)==0 && LOGFILE_ROTATION > 0) {
+                               int i;
+                               char old_fname[1024];
+                               char new_fname[1024];
+
+                               /* Rotate away existing files.  We make sure to remove the
+                                old files otherwise rename() would not work properly.  We
+                                have to loop in reverse here. */
+                               for (i=LOGFILE_ROTATION-1;i>=0;i--) {
+                                       snprintf(old_fname, sizeof(old_fname), "%s%c%s.%d",
+                                               _logdir, PATH_SEPARATOR, dst_fname, i);
                                        snprintf(new_fname, sizeof(new_fname), "%s%c%s.%d",
-                                               _logdir, PATH_SEPARATOR, dst_fname, 0);
-                                       if (ortp_file_exist(new_fname)==0)
-                                               unlink(new_fname);
-                                       rename(_logfname, new_fname);
+                                               _logdir, PATH_SEPARATOR, dst_fname, i+1);
+                                       if (ortp_file_exist(old_fname)==0) {
+                                               if (ortp_file_exist(new_fname)==0)
+                                                       unlink(new_fname);
+                                               rename(old_fname, new_fname);
+                                       }
                                }
-                               /* Start a new log file and mark that we have now initialised */
-                               _logfile = fopen(_logfname, "w");
-                               fprintf(_logfile, "%s\n", LOGFILE_MARKER_START);
+                               /* Move current log file as the first of the rotation.  Make
+                                sure to remove the old .0 also, since otherwise rename()
+                                would not work as expected. */
+                               snprintf(new_fname, sizeof(new_fname), "%s%c%s.%d",
+                                       _logdir, PATH_SEPARATOR, dst_fname, 0);
+                               if (ortp_file_exist(new_fname)==0)
+                                       unlink(new_fname);
+                               rename(_logfname, new_fname);
                        }
+                       /* Start a new log file and mark that we have now initialised */
+                       _logfile = fopen(_logfname, "w");
+                       fprintf(_logfile, "%s\n", LOGFILE_MARKER_START);
                }
                _log_init = TRUE;
        }
@@ -152,33 +164,18 @@ static FILE *linphone_gtk_log_init()
 
 static void linphone_gtk_log_file(OrtpLogLevel lev, const char *msg)
 {
-       LinphoneCore *lc;
        time_t now;
        FILE *outlog;
 
-       lc = linphone_gtk_get_core();
-       /* Nothing to do until the core has initialised */
-       if (lc == NULL)
-               return;
-
-       /* lc->config will turn NULL at exit, close the file to flush and
-        return to stop logging */
-       if (linphone_core_get_config(lc) == NULL) {
-               linphone_gtk_log_uninit();
-               return;
-       }
-
        outlog = linphone_gtk_log_init();
        if (outlog != NULL) {
                /* We have an opened file and we have initialised properly, it's
                 time to write all these log messages. We convert the log level
                 from oRTP into something readable and timestamp each log
-                message.  The format of the timestamp can be controlled by
+                message.  The format of the time       stamp can be controlled by
                 logfile_date_format in the GtkUi section of the config file,
                 but it defaults to something compact, but yet readable. */
                const char *lname="undef";
-               const char *dateformat=linphone_gtk_get_ui_config("logfile_date_format",
-                   "%Y%m%d-%H:%M:%S");
                char date[256];
 
                /* Convert level constant to text */
@@ -210,6 +207,7 @@ static void linphone_gtk_log_file(OrtpLogLevel lev, const char *msg)
                 case of a crash (which is one of the main reasons we have a
                     log facility in the first place). */
                fprintf(outlog, "[%s] [%s] %s\n", date, lname, msg);
+               fflush(outlog);
        }
 }
 
@@ -243,18 +241,12 @@ void linphone_gtk_log_show(void){
        gtk_window_present(GTK_WINDOW(log_window));
 }
 
-static void linphone_gtk_display_log(OrtpLogLevel lev, const char *msg){
+static void linphone_gtk_display_log(GtkTextView *v, OrtpLogLevel lev, const char *msg){
        GtkTextIter iter,begin;
        int off;
-       static GtkTextView *v=NULL;
        GtkTextBuffer *b;
        const char *lname="undef";
 
-       if (log_window==NULL) {
-               return;
-       }
-
-       if (v==NULL) v=GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview"));
        b=gtk_text_view_get_buffer(v);
        switch(lev){
                case ORTP_DEBUG:
@@ -275,6 +267,7 @@ static void linphone_gtk_display_log(OrtpLogLevel lev, const char *msg){
                default:
                        g_error("Bad level !");
        }
+
        gtk_text_buffer_get_end_iter(b,&iter);
        off=gtk_text_iter_get_offset(&iter);
        gtk_text_buffer_insert(b,&iter,lname,-1);
@@ -288,25 +281,59 @@ static void linphone_gtk_display_log(OrtpLogLevel lev, const char *msg){
        gtk_text_buffer_get_iter_at_offset(b,&begin,off);
        if (lev==ORTP_ERROR || lev==ORTP_FATAL) gtk_text_buffer_apply_tag_by_name(b,"red",&begin,&iter);
        else if (lev==ORTP_WARNING) gtk_text_buffer_apply_tag_by_name(b,"orange",&begin,&iter);
+       
+       while(gtk_text_buffer_get_char_count(b)>LOG_MAX_CHARS){
+               GtkTextIter iter_line_after;
+               gtk_text_buffer_get_start_iter(b,&iter);
+               iter_line_after=iter;
+               if (gtk_text_iter_forward_line(&iter_line_after)){
+                       gtk_text_buffer_delete(b,&iter,&iter_line_after);
+               }
+       }
+       
+}
+
+static void stick_to_end(GtkTextView *v){
+       GtkTextBuffer *b;
+       GtkTextIter iter;
+       b=gtk_text_view_get_buffer(v);
        gtk_text_buffer_get_end_iter(b,&iter);
-       //gtk_text_view_scroll_to_iter(v,&iter,0,FALSE,0,0);
+       gtk_text_view_scroll_to_iter(v,&iter,0,FALSE,1.0,0);
+}
+
+void linphone_gtk_log_scroll_to_end(GtkToggleButton *button){
+       if (gtk_toggle_button_get_active(button)){
+               GtkTextView *v=GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview"));
+               stick_to_end(v);
+       }
 }
 
+/*
+ * called from Gtk main loop.
+**/
 gboolean linphone_gtk_check_logs(){
        GList *elem;
+       GtkTextView *v=NULL;
+       if (log_window) v=GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview"));
        g_static_mutex_lock(&log_mutex);
        for(elem=log_queue;elem!=NULL;elem=elem->next){
                LinphoneGtkLog *lgl=(LinphoneGtkLog*)elem->data;
-               linphone_gtk_display_log(lgl->lev,lgl->msg);
+               if (v) linphone_gtk_display_log(v,lgl->lev,lgl->msg);
                g_free(lgl->msg);
                g_free(lgl);
        }
        if (log_queue) g_list_free(log_queue);
        log_queue=NULL;
        g_static_mutex_unlock(&log_mutex);
+       if (v)
+               linphone_gtk_log_scroll_to_end(GTK_TOGGLE_BUTTON(linphone_gtk_get_widget(log_window,"scroll_to_end")));
        return TRUE;
 }
 
+
+/*
+ * Called from any linphone thread.
+ */
 void linphone_gtk_log_push(OrtpLogLevel lev, const char *fmt, va_list args){
        gchar *msg=g_strdup_vprintf(fmt,args);
        LinphoneGtkLog *lgl=g_new(LinphoneGtkLog,1);
@@ -318,3 +345,17 @@ void linphone_gtk_log_push(OrtpLogLevel lev, const char *fmt, va_list args){
        g_static_mutex_unlock(&log_mutex);
 }
 
+void linphone_gtk_log_clear(void){
+       if (log_window){
+               GtkTextIter end,begin;
+               GtkTextView *v;
+               GtkTextBuffer *b;
+               v=GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview"));
+               b=gtk_text_view_get_buffer(v);
+               gtk_text_buffer_get_start_iter(b,&begin);
+               gtk_text_buffer_get_end_iter(b,&end);
+               gtk_text_buffer_delete(b,&begin,&end);
+       }
+}
+
+