summaryrefslogtreecommitdiffstats
path: root/src/common/global/log.h
blob: 7383d57c5e3992daa1cc5e60dc6ee9b69c594bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
 *   Copyright (C) 2005 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#ifndef LOG_H
#define LOG_H

#include <qstringlist.h>

#include "common/common/key_enum.h"

namespace Log
{
  // immediat visibily by calling processEvent... BEWARE of side effects
  enum Action { Immediate, Delayed };
  enum ShowMode { DontShow, Show };
  struct LogData {
    const char *key, *label, *color;
    bool bold;
  };
  BEGIN_DECLARE_ENUM(DebugLevel)
    Quiet = 0, Normal, Extra, Max, LowLevel
  END_DECLARE_ENUM(DebugLevel, LogData)
  BEGIN_DECLARE_ENUM(LineType)
    Error = 0, SoftError, Warning, Normal, Information, Command
  END_DECLARE_ENUM(LineType, LogData)

//-----------------------------------------------------------------------------
class Generic
{
public:
  virtual ~Generic() {}
  virtual void log(LineType type, const QString &text, Action action = Immediate) = 0;
  virtual void log(DebugLevel level, const QString &text, Action action = Immediate) = 0;
  virtual void appendToLastLine(const QString &text) = 0;
  virtual void sorry(const QString &message, const QString &details) = 0;
  virtual bool askContinue(const QString &message) = 0;
  virtual void clear() = 0;
  virtual void logUserAbort() = 0;
};

class View : public Generic
{
public:
  View();
  ShowMode showMode(LineType type) const { return _modes[type.type()]; }
  void setShowMode(LineType type, ShowMode mode) { _modes[type.type()] = mode; }
  void setDebugLevel(DebugLevel level);
  virtual void log(LineType type, const QString &text, Action action = Immediate);
  virtual void log(DebugLevel level, const QString &text, Action action = Immediate);
  virtual void logUserAbort();

protected:
  ShowMode _modes[LineType::Nb_Types];
  DebugLevel _debugLevel;

  virtual void updateDebugLevel() {}
  virtual void doLog(LineType type, const QString &text, Action action) = 0;
  virtual void doLog(DebugLevel level, const QString &text, Action action) = 0;
};

//-----------------------------------------------------------------------------
class StringView : public View
{
public:
  StringView() {}
  QString string() const { return _s; }
  virtual void appendToLastLine(const QString &text) { _s += text; }
  virtual void clear() { _s = QString::null; }
  virtual void sorry(const QString &message, const QString &details);
  virtual bool askContinue(const QString &message);

private:
  QString _s;

  virtual void doLog(LineType, const QString &text, Action) { _s += text + "\n"; }
  virtual void doLog(DebugLevel, const QString &text, Action) { _s += text + "\n"; }
};

//-----------------------------------------------------------------------------
class Base : public Generic
{
public:
  Base(Base *parent = 0);
  virtual ~Base();
  void setParent(Base *parent);
  void setView(View *view);
  View *view() { return logData()->view; }

  virtual void log(LineType type, const QString &message, Action action = Immediate);
  virtual void log(DebugLevel level, const QString &message, Action action = Immediate);
  virtual void appendToLastLine(const QString &text);
  void setError(const QString &error) { logData()->error = error; }
  virtual bool hasError() const { return !logData()->error.isNull(); }
  virtual QString error() const { return logData()->error; }
  virtual void resetError() { logData()->error = QString::null; }
  virtual void sorry(const QString &message, const QString &details = QString::null);
  virtual bool askContinue(const QString &message);
  virtual void clear();
  void logUserAbort();

protected:
  Base *_parent;
  class LogData {
  public:
    LogData() : view(0) {}
    QString error;
    View *view;
  };
  LogData *_data;
  LogData *logData() { return _parent ? _parent->logData() : _data; }
  const LogData *logData() const { return _parent ? _parent->logData() : _data; }
};

class KeyList {
public:
  KeyList(const QString &title = QString::null) : _title(title) {}
  void setTitle(const QString &title) { _title = title; }
  void append(const QString &key, const QString &label) { _keys += key; _labels += label; }
  QString text() const;
  void display(Generic &log) const;

private:
  QString     _title;
  QStringList _keys, _labels;
};

} // namespace

#endif