[Tizen.Log] Replace getting filename with a primitive (fast) way
[platform/core/csapi/tizenfx.git] / src / Tizen.Log / Tizen / Log.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.IO;
19 using System.Linq;
20 using System.Runtime.CompilerServices;
21 using System.ComponentModel;
22
23 namespace Tizen
24 {
25     /// <summary>
26     /// Provides methods to print log messages to the Tizen logging system.
27     /// Depending on products, some priorities (e.g., Vervose and Debug) can be disabled by default to prevent too many logs.
28     /// </summary>
29     /// <since_tizen> 3 </since_tizen>
30     public class Log
31     {
32         /// <summary>
33         /// Prints a log message with the VERBOSE priority.
34         /// </summary>
35         /// <since_tizen> 3 </since_tizen>
36         /// <param name="tag">The tag name of the log message.</param>
37         /// <param name="message">The log message to print.</param>
38         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
39         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
40         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
41         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
42         {
43             Print(Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
44         }
45
46         /// <summary>
47         /// Prints a log message with the DEBUG priority.
48         /// </summary>
49         /// <since_tizen> 3 </since_tizen>
50         /// <param name="tag">The tag name of the log message.</param>
51         /// <param name="message">The log message to print.</param>
52         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
53         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
54         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
55         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
56         {
57             Print(Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
58         }
59
60         /// <summary>
61         /// Prints a log message with the INFO priority.
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         /// <param name="tag">The tag name of the log message.</param>
65         /// <param name="message">The log message to print.</param>
66         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
67         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
68         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
69         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
70         {
71             Print(Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
72         }
73
74         /// <summary>
75         /// Prints a log message with the WARNING priority.
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         /// <param name="tag">The tag name of the log message.</param>
79         /// <param name="message">The log message to print.</param>
80         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
81         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
82         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
83         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
84         {
85             Print(Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
86         }
87
88         /// <summary>
89         /// Prints a log message with the ERROR priority.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         /// <param name="tag">The tag name of the log message.</param>
93         /// <param name="message">The log message to print.</param>
94         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
95         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
96         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
97         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
98         {
99             Print(Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
100         }
101
102         /// <summary>
103         /// Prints a log message with the FATAL priority.
104         /// </summary>
105         /// <since_tizen> 3 </since_tizen>
106         /// <param name="tag">The tag name of the log message.</param>
107         /// <param name="message">The log message to print.</param>
108         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
109         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
110         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
111         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
112         {
113             Print(Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
114         }
115
116         static void Print(Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
117         {
118             if (String.IsNullOrEmpty(file))
119             {
120                 Interop.Dlog.Print(priority, tag, "%s", message);
121             }
122             else
123             {
124                 string[] fileslice = file.Split(new char[] { '\\', '/' });
125                 string filename = fileslice.Last();
126                 Interop.Dlog.Print(priority, tag, "%s: %s(%d) > %s", filename, func, line, message);
127             }
128         }
129     }
130
131     /// <summary>
132     /// Provides methods to print log messages to the Tizen logging system.
133     /// </summary>
134     /// <since_tizen> 3 </since_tizen>
135     [EditorBrowsable(EditorBrowsableState.Never)]
136     public class InternalLog
137     {
138         /// <summary>
139         /// Prints a log message with the VERBOSE priority.
140         /// </summary>
141         /// <since_tizen> 3 </since_tizen>
142         /// <param name="tag">The tag name of the log message.</param>
143         /// <param name="message">The log message to print.</param>
144         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
145         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
146         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
147         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
148         {
149             // For internal dlog APIs, Verbose level log is disabled
150             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
151         }
152
153         /// <summary>
154         /// Prints a log message with the DEBUG priority.
155         /// </summary>
156         /// <since_tizen> 3 </since_tizen>
157         /// <param name="tag">The tag name of the log message.</param>
158         /// <param name="message">The log message to print.</param>
159         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
160         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
161         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
162         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
163         {
164             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
165         }
166
167         /// <summary>
168         /// Prints a log message with the INFO priority.
169         /// </summary>
170         /// <since_tizen> 3 </since_tizen>
171         /// <param name="tag">The tag name of the log message.</param>
172         /// <param name="message">The log message to print.</param>
173         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
174         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
175         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
176         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
177         {
178             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
179         }
180
181         /// <summary>
182         /// Prints a log message with the WARNING priority.
183         /// </summary>
184         /// <since_tizen> 3 </since_tizen>
185         /// <param name="tag">The tag name of the log message.</param>
186         /// <param name="message">The log message to print.</param>
187         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
188         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
189         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
190         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
191         {
192             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
193         }
194
195         /// <summary>
196         /// Prints a log message with the ERROR priority.
197         /// </summary>
198         /// <since_tizen> 3 </since_tizen>
199         /// <param name="tag">The tag name of the log message.</param>
200         /// <param name="message">The log message to print.</param>
201         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
202         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
203         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
204         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
205         {
206             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
207         }
208
209         /// <summary>
210         /// Prints a log message with the FATAL priority.
211         /// </summary>
212         /// <since_tizen> 3 </since_tizen>
213         /// <param name="tag">The tag name of the log message.</param>
214         /// <param name="message">The log message to print.</param>
215         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
216         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
217         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
218         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
219         {
220             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
221         }
222
223         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
224         {
225             if (String.IsNullOrEmpty(file))
226             {
227                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s", message);
228             }
229             else
230             {
231                 string[] fileslice = file.Split(new char[] { '\\', '/' });
232                 string filename = fileslice.Last();
233                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > %s", filename, func, line, message);
234             }
235         }
236     }
237
238     /// <summary>
239     /// Provides methods to print log messages to the Tizen logging system.
240     /// </summary>
241     /// <since_tizen> 3 </since_tizen>
242     [EditorBrowsable(EditorBrowsableState.Never)]
243     public class SecureLog
244     {
245         /// <summary>
246         /// Prints a log message with the VERBOSE priority.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         /// <param name="tag">The tag name of the log message.</param>
250         /// <param name="message">The log message to print.</param>
251         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
252         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
253         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
254         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
255         {
256             // For internal dlog APIs, Verbose level log is disabled
257             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
258         }
259
260         /// <summary>
261         /// Prints a log message with the DEBUG priority.
262         /// </summary>
263         /// <since_tizen> 3 </since_tizen>
264         /// <param name="tag">The tag name of the log message.</param>
265         /// <param name="message">The log message to print.</param>
266         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
267         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
268         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
269         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
270         {
271             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
272         }
273
274         /// <summary>
275         /// Prints a log message with the INFO priority.
276         /// </summary>
277         /// <since_tizen> 3 </since_tizen>
278         /// <param name="tag">The tag name of the log message.</param>
279         /// <param name="message">The log message to print.</param>
280         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
281         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
282         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
283         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
284         {
285             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
286         }
287
288         /// <summary>
289         /// Prints a log message with the WARNING priority.
290         /// </summary>
291         /// <since_tizen> 3 </since_tizen>
292         /// <param name="tag">The tag name of the log message.</param>
293         /// <param name="message">The log message to print.</param>
294         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
295         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
296         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
297         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
298         {
299             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
300         }
301
302         /// <summary>
303         /// Prints a log message with the ERROR priority.
304         /// </summary>
305         /// <since_tizen> 3 </since_tizen>
306         /// <param name="tag">The tag name of the log message.</param>
307         /// <param name="message">The log message to print.</param>
308         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
309         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
310         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
311         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
312         {
313             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
314         }
315
316         /// <summary>
317         /// Prints a log message with the FATAL priority.
318         /// </summary>
319         /// <since_tizen> 3 </since_tizen>
320         /// <param name="tag">The tag name of the log message.</param>
321         /// <param name="message">The log message to print.</param>
322         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
323         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
324         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
325         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
326         {
327             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
328         }
329
330         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
331         {
332 #if !DISABLE_SECURELOG
333             if (String.IsNullOrEmpty(file))
334             {
335                 Interop.Dlog.InternalPrint(log_id, priority, tag, "[SECURE_LOG] %s", message);
336             }
337             else
338             {
339                 string[] fileslice = file.Split(new char[] { '\\', '/' });
340                 string filename = fileslice.Last();
341                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > %s", filename, func, line, message);
342             }
343 #endif
344         }
345     }
346 }