18698523108475fcca5e604ed70d366c569f1dc0
[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.Runtime.CompilerServices;
20 using System.ComponentModel;
21
22 namespace Tizen
23 {
24     /// <summary>
25     /// Provides methods to print log messages to the Tizen logging system.
26     /// Depending on products, some priorities (e.g., Vervose and Debug) can be disabled by default to prevent too many logs.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class Log
30     {
31         /// <summary>
32         /// Prints a log message with the VERBOSE priority.
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         /// <param name="tag">The tag name of the log message.</param>
36         /// <param name="message">The log message to print.</param>
37         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
38         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
39         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
40         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
41         {
42             Print(Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
43         }
44
45         /// <summary>
46         /// Prints a log message with the DEBUG priority.
47         /// </summary>
48         /// <since_tizen> 3 </since_tizen>
49         /// <param name="tag">The tag name of the log message.</param>
50         /// <param name="message">The log message to print.</param>
51         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
52         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
53         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
54         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
55         {
56             Print(Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
57         }
58
59         /// <summary>
60         /// Prints a log message with the INFO priority.
61         /// </summary>
62         /// <since_tizen> 3 </since_tizen>
63         /// <param name="tag">The tag name of the log message.</param>
64         /// <param name="message">The log message to print.</param>
65         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
66         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
67         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
68         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
69         {
70             Print(Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
71         }
72
73         /// <summary>
74         /// Prints a log message with the WARNING priority.
75         /// </summary>
76         /// <since_tizen> 3 </since_tizen>
77         /// <param name="tag">The tag name of the log message.</param>
78         /// <param name="message">The log message to print.</param>
79         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
80         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
81         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
82         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
83         {
84             Print(Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
85         }
86
87         /// <summary>
88         /// Prints a log message with the ERROR priority.
89         /// </summary>
90         /// <since_tizen> 3 </since_tizen>
91         /// <param name="tag">The tag name of the log message.</param>
92         /// <param name="message">The log message to print.</param>
93         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
94         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
95         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
96         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
97         {
98             Print(Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
99         }
100
101         /// <summary>
102         /// Prints a log message with the FATAL priority.
103         /// </summary>
104         /// <since_tizen> 3 </since_tizen>
105         /// <param name="tag">The tag name of the log message.</param>
106         /// <param name="message">The log message to print.</param>
107         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
108         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
109         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
110         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
111         {
112             Print(Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
113         }
114
115         static void Print(Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
116         {
117             if (String.IsNullOrEmpty(file))
118             {
119                 Interop.Dlog.Print(priority, tag, "%s", message);
120             }
121             else
122             {
123                 Uri f = new Uri("file://" + file);
124                 Interop.Dlog.Print(priority, tag, "%s: %s(%d) > %s", Path.GetFileName(f.AbsolutePath), func, line, message);
125             }
126         }
127     }
128
129     /// <summary>
130     /// Provides methods to print log messages to the Tizen logging system.
131     /// </summary>
132     /// <since_tizen> 3 </since_tizen>
133     [EditorBrowsable(EditorBrowsableState.Never)]
134     public class InternalLog
135     {
136         /// <summary>
137         /// Prints a log message with the VERBOSE priority.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         /// <param name="tag">The tag name of the log message.</param>
141         /// <param name="message">The log message to print.</param>
142         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
143         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
144         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
145         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
146         {
147             // For internal dlog APIs, Verbose level log is disabled
148             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
149         }
150
151         /// <summary>
152         /// Prints a log message with the DEBUG priority.
153         /// </summary>
154         /// <since_tizen> 3 </since_tizen>
155         /// <param name="tag">The tag name of the log message.</param>
156         /// <param name="message">The log message to print.</param>
157         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
158         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
159         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
160         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
161         {
162             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
163         }
164
165         /// <summary>
166         /// Prints a log message with the INFO priority.
167         /// </summary>
168         /// <since_tizen> 3 </since_tizen>
169         /// <param name="tag">The tag name of the log message.</param>
170         /// <param name="message">The log message to print.</param>
171         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
172         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
173         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
174         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
175         {
176             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
177         }
178
179         /// <summary>
180         /// Prints a log message with the WARNING priority.
181         /// </summary>
182         /// <since_tizen> 3 </since_tizen>
183         /// <param name="tag">The tag name of the log message.</param>
184         /// <param name="message">The log message to print.</param>
185         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
186         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
187         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
188         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
189         {
190             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
191         }
192
193         /// <summary>
194         /// Prints a log message with the ERROR priority.
195         /// </summary>
196         /// <since_tizen> 3 </since_tizen>
197         /// <param name="tag">The tag name of the log message.</param>
198         /// <param name="message">The log message to print.</param>
199         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
200         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
201         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
202         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
203         {
204             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
205         }
206
207         /// <summary>
208         /// Prints a log message with the FATAL priority.
209         /// </summary>
210         /// <since_tizen> 3 </since_tizen>
211         /// <param name="tag">The tag name of the log message.</param>
212         /// <param name="message">The log message to print.</param>
213         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
214         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
215         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
216         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
217         {
218             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
219         }
220
221         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
222         {
223             if (String.IsNullOrEmpty(file))
224             {
225                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s", message);
226             }
227             else
228             {
229                 Uri f = new Uri("file://" + file);
230                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > %s", Path.GetFileName(f.AbsolutePath), func, line, message);
231             }
232         }
233     }
234
235     /// <summary>
236     /// Provides methods to print log messages to the Tizen logging system.
237     /// </summary>
238     /// <since_tizen> 3 </since_tizen>
239     [EditorBrowsable(EditorBrowsableState.Never)]
240     public class SecureLog
241     {
242         /// <summary>
243         /// Prints a log message with the VERBOSE priority.
244         /// </summary>
245         /// <since_tizen> 3 </since_tizen>
246         /// <param name="tag">The tag name of the log message.</param>
247         /// <param name="message">The log message to print.</param>
248         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
249         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
250         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
251         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
252         {
253             // For internal dlog APIs, Verbose level log is disabled
254             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
255         }
256
257         /// <summary>
258         /// Prints a log message with the DEBUG priority.
259         /// </summary>
260         /// <since_tizen> 3 </since_tizen>
261         /// <param name="tag">The tag name of the log message.</param>
262         /// <param name="message">The log message to print.</param>
263         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
264         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
265         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
266         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
267         {
268             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
269         }
270
271         /// <summary>
272         /// Prints a log message with the INFO priority.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         /// <param name="tag">The tag name of the log message.</param>
276         /// <param name="message">The log message to print.</param>
277         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
278         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
279         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
280         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
281         {
282             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
283         }
284
285         /// <summary>
286         /// Prints a log message with the WARNING priority.
287         /// </summary>
288         /// <since_tizen> 3 </since_tizen>
289         /// <param name="tag">The tag name of the log message.</param>
290         /// <param name="message">The log message to print.</param>
291         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
292         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
293         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
294         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
295         {
296             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
297         }
298
299         /// <summary>
300         /// Prints a log message with the ERROR priority.
301         /// </summary>
302         /// <since_tizen> 3 </since_tizen>
303         /// <param name="tag">The tag name of the log message.</param>
304         /// <param name="message">The log message to print.</param>
305         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
306         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
307         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
308         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
309         {
310             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
311         }
312
313         /// <summary>
314         /// Prints a log message with the FATAL priority.
315         /// </summary>
316         /// <since_tizen> 3 </since_tizen>
317         /// <param name="tag">The tag name of the log message.</param>
318         /// <param name="message">The log message to print.</param>
319         /// <param name="file">The source file path of the caller function. This argument will be set automatically by the compiler.</param>
320         /// <param name="func">The function name of the caller function. This argument will be set automatically by the compiler.</param>
321         /// <param name="line">The line number of the calling position. This argument will be set automatically by the compiler.</param>
322         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
323         {
324             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
325         }
326
327         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
328         {
329 #if !DISABLE_SECURELOG
330             if (String.IsNullOrEmpty(file))
331             {
332                 Interop.Dlog.InternalPrint(log_id, priority, tag, "[SECURE_LOG] %s", message);
333             }
334             else
335             {
336                 Uri f = new Uri("file://" + file);
337                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > [SECURE_LOG] %s", Path.GetFileName(f.AbsolutePath), func, line, message);
338             }
339 #endif
340         }
341     }
342 }