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