Refactor code
[platform/core/csapi/tizenfx.git] / src / Tizen / Tizen / LogTraceListener.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9
10 using System;
11 using System.Text;
12 using System.Diagnostics;
13
14 namespace Tizen
15 {
16     /// <summary>
17     /// Directs tracing or debugging output to Tizen logging system.
18     /// </summary>
19     public class LogTraceListener : TraceListener
20     {
21         private string _tagName = null;
22         private readonly StringBuilder _buf = new StringBuilder();
23
24         /// <summary>
25         /// Initializes a new instance of the LogTraceListener class, using the specfied tag name of the logging system.
26         /// </summary>
27         /// <param name="tag">The tag name of the log message.</param>
28         public LogTraceListener(string tag) : base("DLOG")
29         {
30             _tagName = tag;
31         }
32
33         /// <summary>
34         /// The tag name of the log message.
35         /// </summary>
36         public string Tag
37         {
38             get { return _tagName; }
39             set { _tagName = value; }
40         }
41
42         private void WriteImpl(string message)
43         {
44             if (NeedIndent)
45             {
46                 WriteIndent();
47             }
48             _buf.Append(message);
49         }
50
51         /// <summary>
52         /// Writes an error message to the logging system.
53         /// </summary>
54         /// <param name="message">The error message to print.</param>
55         public override void Fail(string message)
56         {
57             Fail(message, null);
58         }
59
60         /// <summary>
61         /// Writes an error message and a detailed error message to the logging system.
62         /// </summary>
63         /// <param name="message">The error message to print.</param>
64         /// <param name="detailMessage">The detailed error message to print.</param>
65         public override void Fail(string message, string detailMessage)
66         {
67             StringBuilder failBuf = new StringBuilder();
68             failBuf.Append("Fail: ");
69             failBuf.Append(message);
70             if (!String.IsNullOrEmpty(detailMessage))
71             {
72                 failBuf.Append(" ");
73                 failBuf.Append(detailMessage);
74             }
75             Log.Error(_tagName, failBuf.ToString(), null);
76         }
77
78         /// <summary>
79         /// Writes a log message to the logging system.
80         /// </summary>
81         /// <param name="message">The log message to print.</param>
82         public override void Write(string message)
83         {
84             WriteImpl(message);
85         }
86
87         /// <summary>
88         /// Writes a log message followed by the current line terminator to the logging system.
89         /// </summary>
90         /// <param name="message">The log message to print.</param>
91         public override void WriteLine(string message)
92         {
93             WriteImpl(message + Environment.NewLine);
94             NeedIndent = true;
95             Flush();
96         }
97
98         /// <summary>
99         /// Causes buffered data to be written to the logging system.
100         /// </summary>
101         public override void Flush()
102         {
103             Log.Debug(_tagName, _buf.ToString(), null);
104             _buf.Clear();
105         }
106     }
107 }