File: reporting.vala

package info (click to toggle)
listaller 0.5.9-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,096 kB
  • ctags: 1,611
  • sloc: xml: 11,195; ansic: 2,298; sh: 1,648; perl: 1,452; cpp: 1,289; java: 157; makefile: 134; cs: 48; python: 24
file content (176 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* reporting.vala
 *
 * Copyright (C) 2012-2014 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

using GLib;

namespace Listaller {

/**
 * Type of a message written to a Listaller Report
 */
public enum ReportMessageType {
	INFO,
	SUGGESTION,
	WARNING,
	CRITICAL,
	ERROR;

	internal string to_string() {
		switch (this) {
			case INFO:
				return ("I");

			case SUGGESTION:
				return ("S");

			case WARNING:
				return ("W");

			case CRITICAL:
				return ("C");

			case ERROR:
				return ("E");

			default:
				return ("X");
		}
	}
}

/**
 * Report generator
 */
public class Report : Object {
	private static Report instance;

	private Array<string> lines; // probably use a HashSet later...
	private bool error_received;
	private bool print_fatal;

	public Report () {
		lines = new Array<string> ();
		error_received = false;
		print_fatal = false; // NOTE: Changed to FALSE by default now

		debug ("New Listaller Report handler created.");
	}

	public bool contains_error () {
		return error_received;
	}

	public bool is_empty () {
		return lines.length == 0;
	}

	public void add_message (ReportMessageType mtype, string message) {
		lock (lines) {
			string prefix = " %s: ".printf (mtype.to_string ());
			lines.append_val ("%s%s".printf (prefix, message));
		}

		// Because errors and warnings might be very important, we also show a message directly
		if (mtype == ReportMessageType.ERROR) {
			lock (error_received)
				error_received = true;
			// LEVEL_ERROR would abort the program, usually. Because errors which were
			// written to the report aren't that crtical for program execution, we
			// just use the CRITICAL level here
			if (print_fatal)
				log ("Report", LogLevelFlags.LEVEL_CRITICAL, message);
		}
		if (!print_fatal)
			return;

		if (mtype == ReportMessageType.CRITICAL)
			log ("Report", LogLevelFlags.LEVEL_CRITICAL, message);
		if (mtype == ReportMessageType.WARNING)
			log ("Report", LogLevelFlags.LEVEL_WARNING, message);
	}

	public void add_info (string message) {
		add_message (ReportMessageType.INFO, message);
	}

	public void add_warning (string message) {
		add_message (ReportMessageType.WARNING, message);
	}

	public void add_error (string message) {
		add_message (ReportMessageType.ERROR, message);
	}

	public string to_string () {
		string str = "";
		lock (lines) {
			for (uint i=0; i < lines.length; i++) {
				str += "%s\n".printf (lines.index (i));
			}
		}

		return str;
	}

	public void clear () {
		lines.set_size (0);
	}

	public void set_print_fatal (bool print_fatal_msg) {
		print_fatal = print_fatal_msg;
		debug ("Changed printing fatal report messages: %i", (int) print_fatal);
	}

	public static Report get_instance () {
		if (Report.instance == null)
			Report.instance = new Report ();
		return Report.instance;
	}

	public static void delete () {
		Report.instance = null;
	}

	public static void log_message (ReportMessageType mtype, string message) {
		get_instance ().add_message (mtype, message);
	}

	public static void log_info (string message) {
		get_instance ().add_info (message);
	}

	public static void log_warning (string message) {
		get_instance ().add_warning (message);
	}

	public static void log_error (string message) {
		get_instance ().add_error (message);
	}

	public static void clear_current () {
		get_instance ().clear ();
	}

	public static void set_print_fatal_msg (bool print_fatal_msg) {
		get_instance ().set_print_fatal (print_fatal_msg);
	}
}

} // End of namespace: Listaller