Back to index

gcompris  8.2.2
log.c
Go to the documentation of this file.
00001 /* gcompris - log.c
00002  *
00003  * Time-stamp: <2003/10/29 18:55:55 bcoudoin>
00004  *
00005  * Copyright (C) 2004 Bruno Coudoin
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation; either version 2 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with this program; if not, write to the Free Software
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 
00023 /* Trace formating
00024  * date,computer,user,board,level,sublevel,status, duration,comment
00025  *
00026  * status can be : PASSED or FAILED
00027  * comment is a free optional string that can describe what went wrong. For
00028  * example, in a reading activity, you could write here the word that make the
00029  * kid fail. It may be usefull for the teacher.
00030  *
00031  *
00032  */
00033 
00034 #include <unistd.h>
00035 #include <glib/gstdio.h>
00036 #include <time.h>
00037 #include <string.h>
00038 
00039 #include <gcompris.h>
00040 #include "profile.h"
00041 
00042 #define KEYLOG_MAX 256
00043 
00044 #ifdef WIN32
00045 static gchar hostname[256]="unknown";
00046 #else
00047 static gchar hostname[256];
00048 #endif
00049 
00050 static gchar         *comment_set;
00051 static gchar          keylog[KEYLOG_MAX];
00052 static GcomprisBoard *gcomprisBoard_set;
00053 static time_t         start_time;
00054 static time_t         start_time_key;
00055 
00056 /* By default, we use local time, not UTC */
00057 #define USE_UTC 0
00058 
00063 void gc_log_start (GcomprisBoard *gcomprisBoard) {
00064 
00065   gcomprisBoard_set = gcomprisBoard;
00066   start_time     = time(NULL);
00067   start_time_key = time(NULL);
00068 
00069 #ifndef WIN32
00070   gethostname(hostname, 256);
00071 #endif
00072 
00073   comment_set = "";
00074   keylog[0]   = '\0';
00075 }
00076 
00085 void gc_log_set_comment (GcomprisBoard *gcomprisBoard, gchar *expected, gchar *got) {
00086 
00087   printf("gc_log_set_comment %s %s\n", expected, got);
00088   if(gcomprisBoard_set != gcomprisBoard) {
00089     return;
00090   }
00091 
00092   if(expected==NULL)
00093     expected="";
00094 
00095   if(got==NULL)
00096     got="";
00097 
00098   /* If We already had a comment, log the previous one */
00099   if(comment_set[0] != '\0') {
00100     gc_log_end(gcomprisBoard, GCOMPRIS_LOG_STATUS_FAILED);
00101   }
00102 
00103   comment_set = g_strdup_printf("%s;%s", expected, got);
00104 }
00105 
00111 void gc_log_set_key (GcomprisBoard *gcomprisBoard, guint keyval) {
00112   char utf8char[6];
00113   int i;
00114   /* get the current time from the Unix kernel */
00115   time_t end_time = time(NULL);
00116   double duration = difftime(end_time,start_time_key);
00117 
00118   if(!g_unichar_isalnum (gdk_keyval_to_unicode (keyval)))
00119     return;
00120 
00121   /* Reset the timer */
00122   start_time_key = end_time;
00123 
00124   /* Should be an easier way to get the UTF-8 code in our string */
00125   for(i=0; i<6; i++)
00126     utf8char[i] = '\0';
00127 
00128   sprintf(utf8char, "%c", gdk_keyval_to_unicode(keyval));
00129 
00130   g_unichar_to_utf8 (gdk_keyval_to_unicode(keyval),
00131                    utf8char);
00132 
00133   if(strlen(keylog)<(KEYLOG_MAX-10)) {
00134     strcat(keylog, utf8char);
00135     printf(" 1 gc_log_set_key %s\n", keylog);
00136     sprintf(keylog+strlen(keylog), "/%d:", (guint)duration);
00137     printf(" 2 gc_log_set_key %s\n", keylog);
00138   }
00139 
00140 }
00141 
00147 void gc_log_end (GcomprisBoard *gcomprisBoard, gchar *status) {
00148   FILE *flog;
00149   gchar *file;
00150 
00151   /* Prepare our log */
00152 
00153   /* The default format for time represenation.  See strftime(3) */
00154   char *fmt = "%F %T";
00155 
00156   char buf[256];
00157 
00158   /* get the current time from the Unix kernel */
00159   time_t end_time = time(NULL);
00160   double duration = difftime(end_time,start_time);
00161 
00162   struct tm *tp;
00163 
00164   GcomprisUser *gcomprisUser = gc_profile_get_current_user();
00165   const char *username = g_get_user_name();
00166 
00167   if(gcomprisBoard_set != gcomprisBoard)
00168     return;
00169 
00170   if(gcomprisUser && gcomprisUser->login)
00171     username = gcomprisUser->login;
00172 
00173   /* and convert it to UTC or local time representation */
00174   if (USE_UTC)
00175     tp = gmtime(&start_time);
00176   else
00177     tp = localtime(&start_time);
00178 
00179   /* convert the time to a string according to the format specification in fmt */
00180   strftime(buf, sizeof(buf), fmt, tp);
00181 
00182   /* Print it out */
00183   if(g_get_home_dir()) {
00184     file = g_strconcat(g_get_home_dir(), "/.gcompris/gcompris.log", NULL);
00185   } else {
00186     /* On WIN98, No home dir */
00187     file = g_strdup("gcompris.log");
00188   }
00189 
00190   flog = g_fopen(file,"a");
00191 
00192   /* date,computer,user,board,level,sublevel,status, duration,comment */
00193   if(flog)
00194     fprintf(flog, "%s;%s;%s;gcompris;%s;%d;%d;%s;%d;%s;%s\n", buf, hostname, username,
00195            gcomprisBoard->name,
00196            gcomprisBoard->level, gcomprisBoard->sublevel,
00197            status,
00198            (guint)duration,
00199            comment_set,
00200            keylog);
00201   else
00202     g_warning("Failed to save the log in file '%s'", file);
00203 
00204   g_warning("%s;%s;%s;gcompris;%s;%d;%d;%s;%d;%s;%s\n", buf, hostname, username,
00205            gcomprisBoard->name,
00206            gcomprisBoard->level, gcomprisBoard->sublevel,
00207            status,
00208            (guint)duration,
00209            comment_set,
00210            keylog);
00211 
00212   if(flog)
00213     fclose(flog);
00214 
00215   g_free(file);
00216 }
00217