~mvo/synaptic/open-cache-recover

« back to all changes in this revision

Viewing changes to gtk/rglogview.cc

  • Committer: Michael Vogt
  • Date: 2012-11-07 21:23:51 UTC
  • Revision ID: michael.vogt@ubuntu.com-20121107212351-sojk3fi75f6cyxph
commit current status, nice rhistory abstraction, crashes in the gzip tagfile reader

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <cstring>
25
25
#include <map>
26
26
#include <apt-pkg/fileutl.h>
 
27
#include <apt-pkg/tagfile.h>
27
28
 
28
29
#include "config.h"
29
30
#include "rglogview.h"
30
31
#include "rgutils.h"
31
32
#include "rconfiguration.h"
 
33
#include "rhistory.h"
32
34
 
33
35
#include "i18n.h"
34
36
 
35
37
enum { COLUMN_LOG_DAY, 
36
38
       COLUMN_LOG_FILENAME, 
37
39
       COLUMN_LOG_TYPE, 
 
40
       COLUMN_OFFSET, 
38
41
       N_LOG_COLUMNS };
39
42
 
40
 
enum { LOG_TYPE_TOPLEVEL, LOG_TYPE_FILE };
 
43
enum { LOG_TYPE_TOPLEVEL, 
 
44
       LOG_TYPE_FILE };
41
45
 
42
46
void RGLogView::readLogs()
43
47
{
47
51
   GtkTreeStore *store = gtk_tree_store_new(N_LOG_COLUMNS, 
48
52
                                            G_TYPE_STRING,
49
53
                                            G_TYPE_STRING,
 
54
                                            G_TYPE_INT,
50
55
                                            G_TYPE_INT);
51
56
   
52
57
   GtkTreeIter month_iter;  /* Parent iter */
54
59
 
55
60
   unsigned int year, month, day, hour, min, sec;
56
61
   char str[128];
57
 
   const gchar *logfile;
58
 
   const gchar *logdir = RLogDir().c_str();
 
62
 
 
63
   string history_log = _config->FindFile("Dir::Log::History");
 
64
   const gchar *logfile = history_log.c_str();
 
65
 
 
66
   RHistory history;
 
67
   while (history.stepToNextEntry() == true)
 
68
   {
 
69
      string start_date = history.getStartDate();
 
70
      if(sscanf(start_date.c_str(), "%4u-%2u-%2u  %2u:%2u:%2u",
 
71
                &year, &month, &day, &hour, &min, &sec) != 6)
 
72
         continue;
 
73
 
 
74
      struct tm t;
 
75
      t.tm_year = year-1900;
 
76
      t.tm_mon = month-1;
 
77
      t.tm_mday = day;
 
78
      t.tm_hour = hour;
 
79
      t.tm_min = min;
 
80
      t.tm_sec = sec;
 
81
      GDate *date = g_date_new_dmy(day, (GDateMonth)month, year);
 
82
      // need to convert here:
 
83
      // glib: 1=Monday to 7=Sunday 
 
84
      // libc: 0=Sunday to 6=Saturday
 
85
      t.tm_wday = g_date_get_weekday(date); 
 
86
      t.tm_wday %= 7;
 
87
 
 
88
      history_key = year*100+month;
 
89
      if(history_map.count(history_key) == 0) {
 
90
         gtk_tree_store_append(store, &month_iter, NULL); 
 
91
         strftime(str, sizeof(str), "%B %Y", &t);
 
92
         gchar *sort_key = g_strdup_printf("%i", history_key);
 
93
         gtk_tree_store_set (store, &month_iter,
 
94
                             COLUMN_LOG_DAY, utf8(str),
 
95
                             COLUMN_LOG_FILENAME, sort_key, 
 
96
                             COLUMN_LOG_TYPE, LOG_TYPE_TOPLEVEL, 
 
97
                             COLUMN_OFFSET, -1,
 
98
                             -1);
 
99
         g_free(sort_key);
 
100
         history_map.insert(make_pair<int,GtkTreeIter>(history_key,month_iter));
 
101
      } else {
 
102
         month_iter = history_map[history_key];
 
103
      }
 
104
 
 
105
      strftime(str, 512, "%x %R", &t);
 
106
      gtk_tree_store_append (store, &date_iter, &month_iter);
 
107
      gtk_tree_store_set (store, &date_iter,
 
108
                          COLUMN_LOG_DAY, utf8(str),
 
109
                          COLUMN_LOG_FILENAME, history.getCurrentLogfileName().c_str(),
 
110
                          COLUMN_LOG_TYPE, LOG_TYPE_FILE, 
 
111
                          COLUMN_OFFSET, history.getOffset(),
 
112
                          -1);
 
113
      g_free(date);
 
114
   }
 
115
 
 
116
#if 0
59
117
   GDir *dir = g_dir_open(logdir, 0, NULL);
60
118
   while((logfile=g_dir_read_name(dir)) != NULL) {
61
119
      if(sscanf(logfile, "%4u-%2u-%2u.%2u%2u%2u.log", 
79
137
      history_key = year*100+month;
80
138
      if(history_map.count(history_key) == 0) {
81
139
         gtk_tree_store_append(store, &month_iter, NULL); 
82
 
         strftime(str, 128, "%B %Y", &t);
 
140
         strftime(str, sizeof(str), "%B %Y", &t);
83
141
         gchar *sort_key = g_strdup_printf("%i", history_key);
84
142
         gtk_tree_store_set (store, &month_iter,
85
143
                             COLUMN_LOG_DAY, utf8(str),
102
160
      g_free(date);
103
161
   }
104
162
   g_dir_close(dir);
 
163
#endif
105
164
 
106
165
   GtkTreeModel *sort_model;
107
166
   /* Create the first tree */
131
190
   GtkTreeModel *model;
132
191
   GtkTextIter start, end;
133
192
   gchar *file = NULL;
 
193
   int offset = 0;
134
194
 
135
195
   if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
136
196
         GtkTextBuffer *buffer;
137
197
         GtkTextIter start,end;
138
198
 
139
199
         gtk_tree_model_get (model, &iter, COLUMN_LOG_FILENAME, &file, -1);
 
200
         gtk_tree_model_get (model, &iter, COLUMN_OFFSET, &offset, -1);
 
201
 
140
202
         // the months do not have a valid file 
141
 
         if(!FileExists(RLogDir()+string(file)))
 
203
         if (!FileExists(string(file)))
142
204
            return;
143
205
 
 
206
         // FIXME: port to RHistory
 
207
         FileFd Fd;
 
208
         pkgTagSection Section;
 
209
         Fd.Open(file, FileFd::ReadOnly);
 
210
         pkgTagFile Parser(&Fd);
 
211
         Parser.Jump(Section, offset);
 
212
         
144
213
         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(me->_textView));
145
214
         gtk_text_buffer_get_start_iter (buffer, &start);
146
215
         gtk_text_buffer_get_end_iter(buffer,&end);
147
216
         gtk_text_buffer_delete(buffer,&start,&end);
148
 
   
149
 
         string logfile = RLogDir() + string(file);
 
217
 
 
218
         const char *pstart, *pend;
 
219
         char buf[1024];
 
220
         Section.GetSection(pstart, pend);
 
221
         strncpy(buf, pstart, std::min(unsigned long(pend-pstart), sizeof(buf)));
 
222
         buf[pend-pstart] = 0;
 
223
         gtk_text_buffer_set_text(buffer, buf, -1);
 
224
#if 0   
 
225
         string logfile = string(file);
150
226
         ifstream in(logfile.c_str());
151
227
         string s;
152
 
         while(getline(in, s)) {
 
228
         while(getline(in, s)) 
 
229
         {
153
230
            // no need to free str later, it is allocated in a static buffer
154
231
            const char *str = utf8(s.c_str());
155
 
            if(str!=NULL) {
 
232
            if(str!=NULL)
 
233
            {
156
234
               gtk_text_buffer_get_end_iter(buffer, &end);
157
235
               int line = gtk_text_iter_get_line(&end);
158
236
               gtk_text_buffer_insert_at_cursor(buffer, str, -1);
159
 
               if(me->findStr) {
 
237
               if(me->findStr)
 
238
               {
160
239
                  char *off = g_strstr_len(str, strlen(str), me->findStr);
161
 
                  if(off) {
 
240
                  if(off) 
 
241
                  {
162
242
                     gtk_text_buffer_get_iter_at_line_index(buffer, &start, 
163
243
                                                            line, off-str);
164
244
                     gtk_text_buffer_get_iter_at_line_index(buffer, &end, 
167
247
                                                &start, &end);
168
248
                  }
169
249
               } 
170
 
 
171
250
            }
172
251
            gtk_text_buffer_insert_at_cursor(buffer, "\n", -1);
173
252
         }
 
253
#endif
174
254
         g_free(file);
175
255
   }
176
256
}