Release 4.0.0-preview1-00051
[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     [EditorBrowsable(EditorBrowsableState.Never)]
128     public class InternalLog
129     {
130         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
131         {
132             // For internal dlog APIs, Verbose level log is disabled
133             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
134         }
135
136         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
137         {
138             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
139         }
140
141         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
142         {
143             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
144         }
145
146         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
147         {
148             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
149         }
150
151         public static void Error(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
152         {
153             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_ERROR, tag, message, file, func, line);
154         }
155
156         public static void Fatal(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
157         {
158             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_FATAL, tag, message, file, func, line);
159         }
160
161         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
162         {
163             if (String.IsNullOrEmpty(file))
164             {
165                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s", message);
166             }
167             else
168             {
169                 Uri f = new Uri("file://" + file);
170                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > %s", Path.GetFileName(f.AbsolutePath), func, line, message);
171             }
172         }
173     }
174
175     [EditorBrowsable(EditorBrowsableState.Never)]
176     public class SecureLog
177     {
178         public static void Verbose(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
179         {
180             // For internal dlog APIs, Verbose level log is disabled
181             // Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_VERBOSE, tag, message, file, func, line);
182         }
183
184         public static void Debug(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
185         {
186             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_DEBUG, tag, message, file, func, line);
187         }
188
189         public static void Info(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
190         {
191             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_INFO, tag, message, file, func, line);
192         }
193
194         public static void Warn(string tag, string message, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
195         {
196             Print(Interop.Dlog.LogID.LOG_ID_MAIN, Interop.Dlog.LogPriority.DLOG_WARN, tag, message, file, func, line);
197         }
198
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         public static void Fatal(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_FATAL, tag, message, file, func, line);
207         }
208
209         static void Print(Interop.Dlog.LogID log_id, Interop.Dlog.LogPriority priority, string tag, string message, string file, string func, int line)
210         {
211 #if !DISABLE_SECURELOG
212             if (String.IsNullOrEmpty(file))
213             {
214                 Interop.Dlog.InternalPrint(log_id, priority, tag, "[SECURE_LOG] %s", message);
215             }
216             else
217             {
218                 Uri f = new Uri("file://" + file);
219                 Interop.Dlog.InternalPrint(log_id, priority, tag, "%s: %s(%d) > [SECURE_LOG] %s", Path.GetFileName(f.AbsolutePath), func, line, message);
220             }
221 #endif
222         }
223     }
224 }