forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGlobalConfigManager.cs
More file actions
140 lines (121 loc) · 3.96 KB
/
Copy pathGlobalConfigManager.cs
File metadata and controls
140 lines (121 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
namespace Mediapipe.Unity
{
public static class GlobalConfigManager
{
private const string _TAG = nameof(GlobalConfigManager);
private static string CacheDirPath => Path.Combine(Application.persistentDataPath, "Cache");
private static string ConfigFilePath => Path.Combine(CacheDirPath, "globalConfig.env");
private const string _GlogLogtostderrKey = "GLOG_logtostderr";
private const string _GlogStderrthresholdKey = "GLOG_stderrthreshold";
private const string _GlogMinloglevelKey = "GLOG_minloglevel";
private const string _GlogVKey = "GLOG_v";
private const string _GlogLogDirKey = "GLOG_log_dir";
public static bool GlogLogtostderr
{
get => Config[_GlogLogtostderrKey] == "1";
set => Config[_GlogLogtostderrKey] = value ? "1" : "0";
}
public static int GlogStderrthreshold
{
get => int.Parse(Config[_GlogStderrthresholdKey]);
set => Config[_GlogStderrthresholdKey] = value.ToString();
}
public static int GlogMinloglevel
{
get => int.Parse(Config[_GlogMinloglevelKey]);
set => Config[_GlogMinloglevelKey] = value.ToString();
}
public static int GlogV
{
get => int.Parse(Config[_GlogVKey]);
set => Config[_GlogVKey] = value.ToString();
}
public static string GlogLogDir
{
get => Config[_GlogLogDirKey];
set => Config[_GlogLogDirKey] = value;
}
private static Dictionary<string, string> _Config;
private static Dictionary<string, string> Config
{
get
{
if (_Config == null)
{
_Config = new Dictionary<string, string>() {
{ _GlogLogtostderrKey, "1" },
{ _GlogStderrthresholdKey, "2" },
{ _GlogMinloglevelKey, "0" },
{ _GlogLogDirKey, "" },
{ _GlogVKey, "0" },
};
if (!File.Exists(ConfigFilePath))
{
Logger.LogDebug(_TAG, $"Global config file does not exist: {ConfigFilePath}");
}
else
{
Logger.LogDebug(_TAG, $"Reading the config file ({ConfigFilePath})...");
foreach (var line in File.ReadLines(ConfigFilePath))
{
try
{
var pair = ParseLine(line);
_Config[pair.Item1] = pair.Item2;
}
catch (System.Exception e)
{
Logger.LogWarning($"{e}");
}
}
}
}
return _Config;
}
}
public static void Commit()
{
string[] lines = {
$"{_GlogLogtostderrKey}={(GlogLogtostderr ? "1" : "0")}",
$"{_GlogStderrthresholdKey}={GlogStderrthreshold}",
$"{_GlogMinloglevelKey}={GlogMinloglevel}",
$"{_GlogLogDirKey}={GlogLogDir}",
$"{_GlogVKey}={GlogV}",
};
if (!Directory.Exists(CacheDirPath))
{
var _ = Directory.CreateDirectory(CacheDirPath);
}
File.WriteAllLines(ConfigFilePath, lines, Encoding.UTF8);
Logger.LogInfo(_TAG, "Global config file has been updated");
}
public static void SetFlags()
{
Glog.Logtostderr = GlogLogtostderr;
Glog.Stderrthreshold = GlogStderrthreshold;
Glog.Minloglevel = GlogMinloglevel;
Glog.V = GlogV;
Glog.LogDir = GlogLogDir == "" ? null : Path.Combine(Application.persistentDataPath, GlogLogDir);
}
private static (string, string) ParseLine(string line)
{
var i = line.IndexOf('=');
if (i < 0)
{
throw new System.FormatException("Each line in global config file must include '=', but not found");
}
var key = line.Substring(0, i);
var value = line.Substring(i + 1);
return (key, value);
}
}
}