~ubuntu-branches/ubuntu/saucy/f-spot/saucy

« back to all changes in this revision

Viewing changes to src/Utils/Log.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-05-24 10:35:57 UTC
  • mfrom: (2.4.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20100524103557-1j0i8f66caybci2n
Tags: 0.7.0-1
* New upstream release 0.7.0
 + First release of the unstable 0.7 development series. Massive changes.
 + Reparenting and detaching support (Anton Keks) (Closes: #559745)
 + A new Mallard-based documentation (Harold Schreckengost)
 + No longer embeds flickrnet, uses distribution copy (Iain Lane)
 + Adoption of a large amount of Hyena functionality (Paul Lange, Peter
   Goetz)
 + No longer embeds gnome-keyring-sharp
 + Completely rewritten import, much faster and less memory hungry (Ruben
   Vermeersch) (Closes: #559080, #492658, #341790, #357811, #426017) (LP:
   #412091)
 + No longer use gphoto2-sharp, now uses gvfs which is less crash-pron
   (Ruben Vermeersch)
 + Fix Facebook support (Ruben Vermeersch)
 + Modernized unit tests
 + Revamped build (Mike Gemünde)
 + Much improved duplicate detection (much faster too) (Ruben Vermeersch)
 + Mouse selection in Iconview (Vincent Pomey)
 + Image panning support using middle mouse button (Wojciech Dzierżanowski)
 + Timeline slider now restricted to the size of the window (Iain Churcher)
 + Over 100 bugs closed (http://bit.ly/cyVjnD)
   - No more warnings about schema defaults (Closes: #584215) (LP: #586132)
* debian/control: Clean up build deps to match configure checks
* debian/rules: Don't run dh_makeshilbs as we don't ship any shared
  libraries. There are some private ones though, which get picked up and
  result in a useless postinst/postrm call to ldconfig. Thanks, lintian.
* debian/patches/debian_fix-distclean.patch,
  debian/patches/debian_fix_f-spot.exe.config.patch,
  debian/patches/debian_link-system-flickrnet.patch,
  debian/patches/debian_link-system-gnome-keyring.patch,
  debian/patches/debian_disable-unit-tests,
  debian/patches/git_transition_duration.patch,
  debian/patches/ubuntu_fix_folder_export_hang.patch:
  Clean up obsolete patches which are no longer necessary 
* debian/patches/*: Temporarily disable patches which originated from Ubuntu
  and no longer apply cleanly. We will get these back in a future upstream
  development release.
* debian/patches/*: Refresh to apply cleanly 
* debian/rules: Add new include dir to autoreconf call to pick up f-spot
  macros 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// Log.cs
3
 
//
4
 
// Author:
5
 
//   Aaron Bockover <abockover@novell.com>
6
 
//
7
 
// Copyright (C) 2005-2007 Novell, Inc.
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
//
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
//
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Text;
31
 
using System.Collections.Generic;
32
 
 
33
 
namespace FSpot.Utils
34
 
{
35
 
    public class LogNotifyEventArgs : EventArgs
36
 
    {
37
 
        private LogEntry entry;
38
 
        
39
 
        public LogNotifyEventArgs (LogEntry entry)
40
 
        {
41
 
            this.entry = entry;
42
 
        }
43
 
        
44
 
        public LogEntry Entry {
45
 
            get { return entry; }
46
 
        }
47
 
    }
48
 
        
49
 
    public enum LogEntryType
50
 
    {
51
 
        Trace,
52
 
        Debug,
53
 
        Warning,
54
 
        Error,
55
 
        Information
56
 
    }
57
 
    
58
 
    public class LogEntry
59
 
    {
60
 
        private LogEntryType type;
61
 
        private string message;
62
 
        private string details;
63
 
        private DateTime timestamp;
64
 
        
65
 
        internal LogEntry (LogEntryType type, string message, string details)
66
 
        {
67
 
            this.type = type;
68
 
            this.message = message;
69
 
            this.details = details;
70
 
            this.timestamp = DateTime.Now;
71
 
        }
72
 
 
73
 
        public LogEntryType Type { 
74
 
            get { return type; }
75
 
        }
76
 
        
77
 
        public string Message { 
78
 
            get { return message; } 
79
 
        }
80
 
        
81
 
        public string Details { 
82
 
            get { return details; } 
83
 
        }
84
 
 
85
 
        public DateTime TimeStamp { 
86
 
            get { return timestamp; } 
87
 
        }
88
 
    }
89
 
    
90
 
    public static class Log
91
 
    {
92
 
        public static event EventHandler<LogNotifyEventArgs> Notify;
93
 
        
94
 
        private static Dictionary<uint, DateTime> timers = new Dictionary<uint, DateTime> ();
95
 
        private static uint next_timer_id = 1;
96
 
 
97
 
        private static bool debugging = false;
98
 
        public static bool Debugging {
99
 
            get { return debugging; }
100
 
            set { debugging = value; }
101
 
        }
102
 
 
103
 
        private static bool tracing = false;
104
 
        public static bool Tracing {
105
 
            get { return tracing; }
106
 
            set { tracing = value; }
107
 
        }
108
 
        
109
 
        public static void Commit (LogEntryType type, string message, string details, bool showUser)
110
 
        {
111
 
            if (type == LogEntryType.Debug && !Debugging) {
112
 
                return;
113
 
            }
114
 
 
115
 
            if (type == LogEntryType.Trace && !Tracing) {
116
 
                return;
117
 
            }
118
 
        
119
 
            if (type != LogEntryType.Information || (type == LogEntryType.Information && !showUser)) {
120
 
                switch (type) {
121
 
                    case LogEntryType.Error: ConsoleCrayon.ForegroundColor = ConsoleColor.Red; break;
122
 
                    case LogEntryType.Warning: ConsoleCrayon.ForegroundColor = ConsoleColor.Yellow; break;
123
 
                    case LogEntryType.Information: ConsoleCrayon.ForegroundColor = ConsoleColor.Green; break;
124
 
                    case LogEntryType.Debug: ConsoleCrayon.ForegroundColor = ConsoleColor.Blue; break;
125
 
                    case LogEntryType.Trace: ConsoleCrayon.ForegroundColor = ConsoleColor.Magenta; break;
126
 
                }
127
 
                
128
 
                Console.Write ("[{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString (type), DateTime.Now.Hour,
129
 
                    DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
130
 
                
131
 
                ConsoleCrayon.ResetColor ();
132
 
                               
133
 
                if (details != null) {
134
 
                    Console.WriteLine (" {0} - {1}", message, details);
135
 
                } else {
136
 
                    Console.WriteLine (" {0}", message);
137
 
                }
138
 
            }
139
 
            
140
 
            if (showUser) {
141
 
                OnNotify (new LogEntry (type, message, details));
142
 
            }
143
 
 
144
 
            if (type == LogEntryType.Trace) {
145
 
                string str = String.Format ("MARK: {0}: {1}", message, details);
146
 
                Mono.Unix.Native.Syscall.access(str, Mono.Unix.Native.AccessModes.F_OK);
147
 
            }
148
 
        }
149
 
 
150
 
        private static string TypeString (LogEntryType type)
151
 
        {
152
 
            switch (type) {
153
 
                case LogEntryType.Debug:         return "Debug";
154
 
                case LogEntryType.Warning:       return "Warn ";
155
 
                case LogEntryType.Error:         return "Error";
156
 
                case LogEntryType.Information:   return "Info ";
157
 
                case LogEntryType.Trace:         return "Trace";
158
 
            }
159
 
            return null;
160
 
        }
161
 
        
162
 
        private static void OnNotify (LogEntry entry)
163
 
        {
164
 
            EventHandler<LogNotifyEventArgs> handler = Notify;
165
 
            if (handler != null) {
166
 
                handler (null, new LogNotifyEventArgs (entry));
167
 
            }
168
 
        }
169
 
        
170
 
        #region Timer Methods
171
 
        
172
 
        public static uint DebugTimerStart (string message)
173
 
        {
174
 
            return TimerStart (message, false);
175
 
        }
176
 
        
177
 
        public static uint InformationTimerStart (string message)
178
 
        {
179
 
            return TimerStart (message, true);
180
 
        }
181
 
        
182
 
        private static uint TimerStart (string message, bool isInfo)
183
 
        {
184
 
            if (!Debugging && !isInfo) {
185
 
                return 0;
186
 
            }
187
 
            
188
 
            if (isInfo) {
189
 
                Information (message);
190
 
            } else {
191
 
                Debug (message);
192
 
            }
193
 
            
194
 
            return TimerStart (isInfo);
195
 
        }
196
 
        
197
 
        public static uint DebugTimerStart ()
198
 
        {
199
 
            return TimerStart (false);
200
 
        }
201
 
        
202
 
        public static uint InformationTimerStart ()
203
 
        {
204
 
            return TimerStart (true);
205
 
        }
206
 
            
207
 
        private static uint TimerStart (bool isInfo)
208
 
        {
209
 
            if (!Debugging && !isInfo) {
210
 
                return 0;
211
 
            }
212
 
            
213
 
            uint timer_id = next_timer_id++;
214
 
            timers.Add (timer_id, DateTime.Now);
215
 
            return timer_id;
216
 
        }
217
 
        
218
 
        public static void DebugTimerPrint (uint id)
219
 
        {
220
 
            if (!Debugging) {
221
 
                return;
222
 
            }
223
 
            
224
 
            TimerPrint (id, "Operation duration: {0}", false);
225
 
        }
226
 
        
227
 
        public static void DebugTimerPrint (uint id, string message)
228
 
        {
229
 
            if (!Debugging) {
230
 
                return;
231
 
            }
232
 
            
233
 
            TimerPrint (id, message, false);
234
 
        }
235
 
        
236
 
        public static void InformationTimerPrint (uint id)
237
 
        {
238
 
            TimerPrint (id, "Operation duration: {0}", true);
239
 
        }
240
 
        
241
 
        public static void InformationTimerPrint (uint id, string message)
242
 
        {
243
 
            TimerPrint (id, message, true);
244
 
        }
245
 
        
246
 
        private static void TimerPrint (uint id, string message, bool isInfo)
247
 
        {
248
 
            if (!Debugging && !isInfo) {
249
 
                return;
250
 
            }
251
 
            
252
 
            DateTime finish = DateTime.Now;
253
 
            
254
 
            if (!timers.ContainsKey (id)) {
255
 
                return;
256
 
            }
257
 
            
258
 
            TimeSpan duration = finish - timers[id];
259
 
            string d_message;
260
 
            if (duration.TotalSeconds < 60) {
261
 
                d_message = String.Format ("{0}s", duration.TotalSeconds);
262
 
            } else {
263
 
                d_message = duration.ToString ();
264
 
            }
265
 
            
266
 
            if (isInfo) {
267
 
                Information (message, d_message);
268
 
            } else {
269
 
                Debug (message, d_message);
270
 
            }
271
 
        }
272
 
        
273
 
        #endregion
274
 
 
275
 
        #region Public Trace Methods
276
 
        public static void Trace (string group, string format, params object [] args)
277
 
        {
278
 
            if (Tracing) {
279
 
                Commit (LogEntryType.Trace, group, String.Format (format, args), false);
280
 
            }
281
 
        }
282
 
        #endregion
283
 
        
284
 
        #region Public Debug Methods
285
 
        public static void Debug (string message)
286
 
        {
287
 
            if (Debugging) {
288
 
                Commit (LogEntryType.Debug, message, null, false);
289
 
            }
290
 
        }
291
 
 
292
 
                public static void Debug (string format, params object [] args)
293
 
        {
294
 
            if (Debugging) {
295
 
                Debug (String.Format (format, args));
296
 
            }
297
 
        }
298
 
        
299
 
                public static void DebugException (Exception e)
300
 
                {
301
 
                    if (Debugging)
302
 
                        Exception (e);
303
 
                }
304
 
        
305
 
                public static void DebugException (string message, Exception e)
306
 
                {
307
 
                    if (Debugging)
308
 
                        Exception (message, e);
309
 
        
310
 
                }
311
 
        #endregion
312
 
        
313
 
        #region Public Information Methods
314
 
        public static void Information (string message, string details, bool showUser)
315
 
        {
316
 
            Commit (LogEntryType.Information, message, details, showUser);
317
 
        }
318
 
        
319
 
        public static void Information (string message, bool showUser)
320
 
        {
321
 
            Information (message, null, showUser);
322
 
        }
323
 
        
324
 
        public static void Information (string format, params object [] args)
325
 
        {
326
 
            Information (String.Format (format, args), null, false);
327
 
        }
328
 
        #endregion
329
 
        
330
 
        #region Public Warning Methods
331
 
        public static void Warning (string message, string details, bool showUser)
332
 
        {
333
 
            Commit (LogEntryType.Warning, message, details, showUser);
334
 
        }
335
 
        
336
 
        public static void Warning (string message, bool showUser)
337
 
        {
338
 
            Warning (message, null, showUser);
339
 
        }
340
 
 
341
 
                public static void Warning (string format, params object [] args)
342
 
        {
343
 
            Warning (String.Format (format, args), false);
344
 
        }
345
 
        #endregion
346
 
        
347
 
        #region Public Error Methods
348
 
        public static void Error (string message, string details, bool showUser)
349
 
        {
350
 
            Commit (LogEntryType.Error, message, details, showUser);
351
 
        }
352
 
        
353
 
        public static void Error (string message, bool showUser)
354
 
        {
355
 
            Error (message, null, showUser);
356
 
        }
357
 
 
358
 
        public static void Error (string format, params object [] args)
359
 
        {
360
 
            Error (String.Format (format, args), null, false);
361
 
        }
362
 
        #endregion
363
 
        
364
 
        #region Public Exception Methods
365
 
        
366
 
        public static void Exception (Exception e)
367
 
        {
368
 
            Exception (null, e);
369
 
        }
370
 
        
371
 
        public static void Exception (string message, Exception e)
372
 
        {
373
 
            Stack<Exception> exception_chain = new Stack<Exception> ();
374
 
            StringBuilder builder = new StringBuilder ();
375
 
            
376
 
            while (e != null) {
377
 
                exception_chain.Push (e);
378
 
                e = e.InnerException;
379
 
            }
380
 
            
381
 
            while (exception_chain.Count > 0) {
382
 
                e = exception_chain.Pop ();
383
 
                builder.AppendFormat ("{0} (in `{1}')", e.Message, e.Source).AppendLine ();
384
 
                builder.Append (e.StackTrace);
385
 
                if (exception_chain.Count > 0) {
386
 
                    builder.AppendLine ();
387
 
                }
388
 
            }
389
 
        
390
 
            // FIXME: We should save these to an actual log file
391
 
            Log.Warning (message ?? "Caught an exception", builder.ToString (), false);
392
 
        }
393
 
                
394
 
                public static void Exception (Exception e, string format, params object [] args)
395
 
                {
396
 
                        Exception (String.Format (format, args), e);
397
 
                }
398
 
                
399
 
        #endregion
400
 
    }
401
 
}