/* * Display pending messages in the LogList */ void Log_Print_Tmp_List (void) { GtkTreeIter iter; LogPrintTmpList = g_list_first(LogPrintTmpList); while (LogPrintTmpList) { if (LogList && logListModel) { LogListNbrRows++; gtk_list_store_append(logListModel, &iter); gtk_list_store_set(logListModel, &iter, LOG_PIXBUF, Log_Get_Stock_Id_From_Error_Type( ((Log_Data *)LogPrintTmpList->data)->error_type ), LOG_TIME_TEXT, ((Log_Data *)LogPrintTmpList->data)->time, LOG_TEXT, ((Log_Data *)LogPrintTmpList->data)->string, LOG_ROW_BACKGROUND, NULL, LOG_ROW_FOREGROUND, NULL, -1); Log_List_Set_Row_Visible(GTK_TREE_MODEL(logListModel), &iter); } if (!LogPrintTmpList->next) break; LogPrintTmpList = LogPrintTmpList->next; } // Free the list... if (LogPrintTmpList) { LogPrintTmpList = g_list_first(LogPrintTmpList); while (LogPrintTmpList) { g_free(((Log_Data *)LogPrintTmpList->data)->time); g_free(((Log_Data *)LogPrintTmpList->data)); if (!LogPrintTmpList->next) break; LogPrintTmpList = LogPrintTmpList->next; } g_list_free(LogPrintTmpList); LogPrintTmpList = NULL; } }
/* * Display pending messages in the LogList */ static void Log_Print_Tmp_List (void) { GList *l; GtkTreeIter iter; LogPrintTmpList = g_list_first (LogPrintTmpList); for (l = LogPrintTmpList; l != NULL; l = g_list_next (l)) { if (LogList && logListModel) { gtk_list_store_insert_with_values (logListModel, &iter, G_MAXINT, LOG_PIXBUF, Log_Get_Stock_Id_From_Error_Type (((Log_Data *)l->data)->error_type), LOG_TIME_TEXT, ((Log_Data *)l->data)->time, LOG_TEXT, ((Log_Data *)l->data)->string, LOG_ROW_BACKGROUND, NULL, LOG_ROW_FOREGROUND, NULL, -1); Log_List_Set_Row_Visible(GTK_TREE_MODEL(logListModel), &iter); } } // Free the list... if (LogPrintTmpList) { GList *l; for (l = LogPrintTmpList; l != NULL; l = g_list_next (l)) { g_free (((Log_Data *)l->data)->string); g_free (((Log_Data *)l->data)->time); g_free (((Log_Data *)l->data)); } g_list_free (LogPrintTmpList); LogPrintTmpList = NULL; } }
/* * Function to use anywhere in the application to send a message to the LogList */ void Log_Print (Log_Error_Type error_type, gchar const *format, ...) { va_list args; gchar *string; GtkTreeIter iter; static gboolean first_time = TRUE; static gchar *file_path = NULL; GFile *file; GFileOutputStream *file_ostream; GError *error = NULL; va_start (args, format); string = g_strdup_vprintf(format, args); va_end (args); // If the log window is displayed then messages are displayed, else // the messages are stored in a temporary list. if (LogList && logListModel) { gint n_items; gchar *time = Log_Format_Date(); /* Remove lines that exceed the limit. */ n_items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (logListModel), NULL); if (n_items > LOG_MAX_LINES - 1 && gtk_tree_model_get_iter_first(GTK_TREE_MODEL(logListModel), &iter)) { gtk_list_store_remove(GTK_LIST_STORE(logListModel), &iter); } gtk_list_store_insert_with_values (logListModel, &iter, G_MAXINT, LOG_PIXBUF, Log_Get_Stock_Id_From_Error_Type (error_type), LOG_TIME_TEXT, time, LOG_TEXT, string, -1); Log_List_Set_Row_Visible(GTK_TREE_MODEL(logListModel), &iter); g_free(time); }else { Log_Data *LogData = g_malloc0(sizeof(Log_Data)); LogData->time = Log_Format_Date(); LogData->error_type = error_type; LogData->string = g_strdup(string); LogPrintTmpList = g_list_append(LogPrintTmpList,LogData); //g_print("%s",string); } // Store also the messages in the log file. if (!file_path) { gchar *cache_path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME, NULL); if (!g_file_test (cache_path, G_FILE_TEST_IS_DIR)) { gint result = g_mkdir_with_parents (cache_path, S_IRWXU); if (result == -1) { g_printerr ("%s", "Unable to create cache directory"); g_free (cache_path); g_free (string); return; } } file_path = g_build_filename (cache_path, LOG_FILE, NULL); g_free (cache_path); } file = g_file_new_for_path (file_path); /* On startup, the log is cleared. The log is then appended to for the * remainder of the application lifetime. */ if (first_time) { file_ostream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error); } else { file_ostream = g_file_append_to (file, G_FILE_CREATE_NONE, NULL, &error); } if (file_ostream) { gchar *time; GString *data; gsize bytes_written; time = Log_Format_Date (); data = g_string_new (time); g_free (time); data = g_string_append_c (data, ' '); data = g_string_append (data, string); g_free (string); data = g_string_append_c (data, '\n'); if (!g_output_stream_write_all (G_OUTPUT_STREAM (file_ostream), data->str, data->len, &bytes_written, NULL, &error)) { g_debug ("Only %" G_GSIZE_FORMAT " bytes out of %" G_GSIZE_FORMAT "bytes of data were written", bytes_written, data->len); /* To avoid recursion of Log_Print. */ g_warning ("Error writing to the log file '%s' ('%s')", file_path, error->message); g_error_free (error); g_string_free (data, TRUE); g_object_unref (file_ostream); g_object_unref (file); return; } first_time = FALSE; g_string_free (data, TRUE); } else { g_warning ("Error opening output stream of file '%s' ('%s')", file_path, error->message); g_error_free (error); } g_object_unref (file_ostream); g_object_unref (file); }
/* * Function to use anywhere in the application to send a message to the LogList */ void Log_Print (Log_Error_Type error_type, gchar const *format, ...) { va_list args; gchar *string; GtkTreeIter iter; static gboolean first_time = TRUE; static gchar *file_path = NULL; FILE *file = NULL; va_start (args, format); string = g_strdup_vprintf(format, args); va_end (args); // If the log window is displayed then messages are displayed, else // the messages are stored in a temporary list. if (LogList && logListModel) { gchar *time = Log_Format_Date(); // Remove lines that exceed the limit if (LogListNbrRows > LOG_MAX_LINES - 1 && gtk_tree_model_get_iter_first(GTK_TREE_MODEL(logListModel), &iter)) { gtk_list_store_remove(GTK_LIST_STORE(logListModel), &iter); } LogListNbrRows++; gtk_list_store_append(logListModel, &iter); gtk_list_store_set(logListModel, &iter, LOG_PIXBUF, Log_Get_Stock_Id_From_Error_Type(error_type), LOG_TIME_TEXT, time, LOG_TEXT, string, LOG_ROW_BACKGROUND, NULL, LOG_ROW_FOREGROUND, NULL, -1); Log_List_Set_Row_Visible(GTK_TREE_MODEL(logListModel), &iter); g_free(time); }else { Log_Data *LogData = g_malloc0(sizeof(Log_Data)); LogData->time = Log_Format_Date(); LogData->error_type = error_type; LogData->string = g_strdup(string); LogPrintTmpList = g_list_append(LogPrintTmpList,LogData); //g_print("%s",string); } // Store also the messages in the log file. if (!file_path) { gchar *cache_path = g_build_filename (g_get_user_cache_dir (), PACKAGE_TARNAME, NULL); if (!g_file_test (cache_path, G_FILE_TEST_IS_DIR)) { gint result = g_mkdir_with_parents (cache_path, S_IRWXU); if (result == -1) { g_printerr ("%s", "Unable to create cache directory"); g_free (cache_path); return; } } file_path = g_build_filename (cache_path, LOG_FILE, NULL); g_free (cache_path); } // The first time, the whole file is deleted. Else, text is appended. if (first_time) file = fopen(file_path,"w+"); else file = fopen(file_path,"a+"); //g_free(file_path); if (file) { gchar *time = Log_Format_Date(); gchar *data = g_strdup_printf("%s %s\n",time,string); fwrite(data,strlen(data),1,file); g_free(data); g_free(time); first_time = FALSE; fclose(file); } g_free(string); }