[NUI] Fix test-hub crash issue
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / DebugFileLogging.cs
1
2 /*
3  * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 using global::System;
20 using global::System.IO;
21 using global::System.Diagnostics;
22
23 namespace Tizen.NUI
24 {
25     internal class DebugFileLogging : Disposable
26     {
27         #region Constant Fields
28         private string logFolderPath;
29         #endregion //Constant Fields
30
31         #region Fields
32         public static DebugFileLogging Instance => instance;
33         private static readonly DebugFileLogging instance = new DebugFileLogging();
34         private string filePath;
35         private FileStream file;
36         #endregion //Fields
37
38         #region Constructors
39         private DebugFileLogging()
40         {
41             if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
42             {
43                 logFolderPath = Environment.GetEnvironmentVariable("HOME") + "/nui/";
44             }
45             else
46             {
47                 logFolderPath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "/owner/share/nui/";
48             }
49
50             Directory.CreateDirectory(logFolderPath);
51
52             var id = Process.GetCurrentProcess().Id;
53             filePath = logFolderPath + id.ToString();
54             file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
55         }
56         #endregion //Constructors
57
58         #region Methods
59         [Conditional("NUI_DEBUG_ON")]
60         internal void WriteLog(string log)
61         {
62             TimeSpan curr = DateTime.Now.TimeOfDay;
63
64             FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write);
65             StreamWriter write = new StreamWriter(file);
66             var time = String.Format("[{0,-13}] ", curr.TotalMilliseconds);
67             write.WriteLine(time + log);
68             write.Close();
69         }
70         #endregion //Methods
71     }
72 }