Skip to content
API Documentation
BastionGuard™
TeeJee.Logging.vala
Go to the documentation of this file.
1
2/*
3 * TeeJee.Logging.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
26
27 /* Functions for logging messages to console and log files */
28
29 using TeeJee.Misc;
30
31 public DataOutputStream dos_log;
32 public string err_log;
33 public bool LOG_ENABLE = true;
34 public bool LOG_TIMESTAMP = false;
35 public bool LOG_COLORS = true;
36 public bool LOG_DEBUG = false;
37 public bool LOG_COMMANDS = false;
38
39 public void log_msg (string message, bool highlight = false){
40
41 if (!LOG_ENABLE) { return; }
42
43 string msg = "";
44
45 if (highlight && LOG_COLORS){
46 msg += "\033[1;38;5;34m";
47 }
48
49 if (LOG_TIMESTAMP){
50 msg += "[" + timestamp(true) + "] ";
51 }
52
53 msg += message;
54
55 if (highlight && LOG_COLORS){
56 msg += "\033[0m";
57 }
58
59 msg += "\n";
60
61 stdout.printf (msg);
62 stdout.flush();
63
64 try {
65 if (dos_log != null){
66 dos_log.put_string ("[%s] %s\n".printf(timestamp(), message));
67 }
68 }
69 catch (Error e) {
70 stdout.printf (e.message);
71 }
72 }
73
74 public void log_error (string message, bool highlight = false,
75 bool is_warning = false){
76
77 if (!LOG_ENABLE) { return; }
78
79 string msg = "";
80
81 if (highlight && LOG_COLORS){
82 msg += "\033[1;38;5;160m";
83 }
84
85 if (LOG_TIMESTAMP){
86 msg += "[" + timestamp(true) + "] ";
87 }
88
89 string prefix = (is_warning) ? "W" : "E";
90
91 msg += prefix + ": " + message;
92
93 if (highlight && LOG_COLORS){
94 msg += "\033[0m";
95 }
96
97 msg += "\n";
98
99 stdout.printf (msg);
100 stdout.flush();
101
102 try {
103 string str = "[%s] %s: %s\n".printf(timestamp(), prefix, message);
104
105 if (dos_log != null){
106 dos_log.put_string (str);
107 }
108
109 if (err_log != null){
110 err_log += "%s\n".printf(message);
111 }
112 }
113 catch (Error e) {
114 stdout.printf (e.message);
115 }
116 }
117
118 public void log_debug (string message){
119 if (!LOG_ENABLE) { return; }
120
121 if (LOG_DEBUG){
122 log_msg ("D: " + message);
123 }
124
125 try {
126 if (dos_log != null){
127 dos_log.put_string ("[%s] %s\n".printf(timestamp(), message));
128 }
129 }
130 catch (Error e) {
131 stdout.printf (e.message);
132 }
133 }
134
135 public void log_to_file (string message, bool highlight = false){
136 try {
137 if (dos_log != null){
138 dos_log.put_string ("[%s] %s\n".printf(timestamp(), message));
139 }
140 }
141 catch (Error e) {
142 stdout.printf (e.message);
143 }
144 }
145}
DataOutputStream dos_log
void log_debug(string message)
void log_error(string message, bool highlight=false, bool is_warning=false)
void log_msg(string message, bool highlight=false)
void log_to_file(string message, bool highlight=false)
string timestamp(bool show_millis=false)