Commit fa528fb5bb97b91b53f250c4f685e1d3d7706ca0

Authored by Perry Werneck
1 parent 37bf41db

Implementing default log file from command line option.

src/include/pw3270/application.h
... ... @@ -73,6 +73,7 @@ GList * pw3270_application_get_keypad_models(GApplication *app);
73 73  
74 74 void pw3270_application_set_ui_style(GApplication *app, PW3270_UI_STYLE type);
75 75 PW3270_UI_STYLE pw3270_application_get_ui_style(GApplication *app);
  76 +void pw3270_application_set_log_filename(GApplication *app, const gchar *filename);
76 77  
77 78 // Plugins
78 79 void pw3270_application_plugin_foreach(GApplication *app, GFunc func, gpointer user_data);
... ...
src/main/main.c
... ... @@ -57,11 +57,6 @@ static gboolean quit_signal(GtkApplication *app) {
57 57 }
58 58 #endif // G_OS_UNIX
59 59  
60   -static void g_log_to_lib3270(const gchar *log_domain,GLogLevelFlags G_GNUC_UNUSED(log_level),const gchar *message,gpointer G_GNUC_UNUSED(user_data)) {
61   - debug("%s",message);
62   - lib3270_write_log(NULL,log_domain ? log_domain : G_STRINGIFY(PRODUCT_NAME),"%s",message);
63   -}
64   -
65 60 int main (int argc, char **argv) {
66 61  
67 62 int status = -1;
... ... @@ -86,8 +81,6 @@ int main (int argc, char **argv) {
86 81 }
87 82 #endif // _WIN32
88 83  
89   - g_log_set_default_handler(g_log_to_lib3270,NULL);
90   -
91 84 bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
92 85 textdomain(PACKAGE_NAME);
93 86  
... ...
src/objects/application/application.c
... ... @@ -56,6 +56,7 @@ struct _pw3270Application {
56 56  
57 57 GSettings * settings;
58 58 GList * keypads;
  59 + gchar * logfile;
59 60 gboolean allow_tabs; ///< @brief Always open window.
60 61  
61 62 GSList * plugins; ///< @brief Handlers of the loaded plugins.
... ... @@ -127,10 +128,22 @@ static void window_removed(GtkApplication *application, GtkWindow *window) {
127 128  
128 129 }
129 130  
  131 +static void g_log_to_lib3270(const gchar *log_domain,GLogLevelFlags G_GNUC_UNUSED(log_level),const gchar *message,gpointer G_GNUC_UNUSED(user_data)) {
  132 + debug("%s",message);
  133 + lib3270_write_log(
  134 + NULL,
  135 + log_domain ? log_domain : G_STRINGIFY(PRODUCT_NAME),
  136 + "%s",
  137 + message
  138 + );
  139 +}
  140 +
130 141 static void pw3270Application_class_init(pw3270ApplicationClass *klass) {
131 142  
132 143 GObjectClass *object_class = G_OBJECT_CLASS(klass);
133 144  
  145 + g_log_set_default_handler(g_log_to_lib3270,NULL);
  146 +
134 147 object_class->get_property = get_property;
135 148 object_class->set_property = set_property;
136 149 object_class->finalize = finalize;
... ... @@ -217,12 +230,6 @@ static gboolean on_user_interface(const gchar G_GNUC_UNUSED(*option), const gcha
217 230  
218 231 }
219 232  
220   -static gboolean on_log_file(const gchar G_GNUC_UNUSED(*option), const gchar *value, gpointer G_GNUC_UNUSED(dunno), GError **error) {
221   - pw3270Application *app = PW3270_APPLICATION(g_application_get_default());
222   -
223   - return TRUE;
224   -}
225   -
226 233 static gboolean on_allow_tabs(const gchar G_GNUC_UNUSED(*option), const gchar *value, gpointer G_GNUC_UNUSED(dunno), GError **error) {
227 234 pw3270Application *app = PW3270_APPLICATION(g_application_get_default());
228 235 app->allow_tabs = (g_ascii_strcasecmp(value,"no") != 0);
... ... @@ -234,13 +241,19 @@ static gboolean on_allow_tabs(const gchar G_GNUC_UNUSED(*option), const gchar *v
234 241 return TRUE;
235 242 }
236 243  
  244 +static gboolean on_logfile(const gchar G_GNUC_UNUSED(*option), const gchar *value, gpointer G_GNUC_UNUSED(dunno), GError **error) {
  245 + pw3270Application *app = PW3270_APPLICATION(g_application_get_default());
  246 + pw3270_application_set_log_filename(app,value);
  247 + return TRUE;
  248 +}
  249 +
237 250 static void pw3270Application_init(pw3270Application *app) {
238 251  
239 252 static GOptionEntry cmd_options[] = {
240 253  
241 254 { "user-interface", 'U', 0, G_OPTION_ARG_CALLBACK, &on_user_interface, N_( "Set the user-interface type" ), NULL },
242   - { "log-filename", 'l', 0, G_OPTION_ARG_CALLBACK, &on_log_file, N_( "Set custom log file for the active session" ), NULL },
243   - { "allow-tabs", 'T', 0, G_OPTION_ARG_CALLBACK, &on_allow_tabs, N_( "If 'no' allways open a window" ), NULL },
  255 + { "allow-tabs", 'T', 0, G_OPTION_ARG_CALLBACK, &on_allow_tabs, N_( "If 'no' allways open a window" ), NULL },
  256 + { "logfile", 'l', 0, G_OPTION_ARG_CALLBACK, &on_logfile, N_( "Set default log file name" ), NULL },
244 257 { NULL }
245 258  
246 259 };
... ... @@ -355,6 +368,13 @@ static void finalize(GObject *object) {
355 368 application->settings = NULL;
356 369 }
357 370  
  371 + lib3270_set_log_handler(NULL,NULL,NULL);
  372 +
  373 + if(application->logfile) {
  374 + g_free(application->logfile);
  375 + application->logfile = NULL;
  376 + }
  377 +
358 378 g_list_free_full(application->keypads,g_object_unref);
359 379  
360 380 G_OBJECT_CLASS(pw3270Application_parent_class)->finalize(object);
... ... @@ -640,3 +660,50 @@ GList * pw3270_application_get_keypad_models(GApplication *app) {
640 660 return PW3270_APPLICATION(app)->keypads;
641 661 }
642 662  
  663 +static int loghandler(const H3270 *hSession, pw3270Application *app, const char *module, int code, const char *message) {
  664 +
  665 + if(!app->logfile) {
  666 + return -1;
  667 + }
  668 +
  669 + FILE *f = fopen(app->logfile,"a");
  670 +
  671 + if(f) {
  672 + time_t ltime = time(0);
  673 +
  674 + char timestamp[80];
  675 +#ifdef HAVE_LOCALTIME_R
  676 + struct tm tm;
  677 + strftime(timestamp, 79, "%x %X", localtime_r(&ltime,&tm));
  678 +#else
  679 + strftime(timestamp, 79, "%x %X", localtime(&ltime));
  680 +#endif // HAVE_LOCALTIME_R
  681 +
  682 + fprintf(f,"%s %s\t%s\n",timestamp,module,message);
  683 +
  684 + fclose(f);
  685 + }
  686 +
  687 + return 0;
  688 +}
  689 +
  690 +void pw3270_application_set_log_filename(GApplication *app, const gchar *filename) {
  691 +
  692 + g_return_if_fail(PW3270_IS_APPLICATION(app));
  693 +
  694 + pw3270Application * application = PW3270_APPLICATION(app);
  695 +
  696 + if(application->logfile) {
  697 + g_free(application->logfile);
  698 + application->logfile = NULL;
  699 + }
  700 +
  701 + if(filename) {
  702 + application->logfile = g_strdup(filename);
  703 + lib3270_set_log_handler(NULL,(LIB3270_LOG_HANDLER) loghandler, app);
  704 + } else {
  705 + lib3270_set_log_handler(NULL,NULL,NULL);
  706 + }
  707 +
  708 +}
  709 +
... ...