Skip to content

Commit bf5d35f

Browse files
committed
logRotateの不具合修正
1 parent 358b6d4 commit bf5d35f

26 files changed

+897
-3973
lines changed

MABProcessAtWait/AppConfig.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class AppConfig {
1515
言語(jp,en)
1616
1717
*/
18+
private static Logger logger = new Logger("MABProcess");
1819
public static string BackupPath { get; set; }
1920
public static Font Font { get; set; }
2021
public static bool DoZip { get; set; }
@@ -49,17 +50,17 @@ public AppConfig() {
4950
AddGameDirPasess.Add(datas[i]);
5051
}
5152
}
52-
Logger.Info("-----loaded appConfig-----");
53-
Logger.Info($"backupPath:{BackupPath}");
54-
Logger.Info($"font:{Font}");
55-
Logger.Info($"dozip:{DoZip}");
56-
Logger.Info($"clientSize:{ClientSize.Width},{ClientSize.Height}");
57-
Logger.Info($"clientPoint:{ClientPoint.X},{ClientPoint.Y}");
58-
Logger.Info($"backupCount:{BackupCount}");
53+
logger.Info("-----loaded appConfig-----");
54+
logger.Info($"backupPath:{BackupPath}");
55+
logger.Info($"font:{Font}");
56+
logger.Info($"dozip:{DoZip}");
57+
logger.Info($"clientSize:{ClientSize.Width},{ClientSize.Height}");
58+
logger.Info($"clientPoint:{ClientPoint.X},{ClientPoint.Y}");
59+
logger.Info($"backupCount:{BackupCount}");
5960
foreach (string path in AddGameDirPasess) {
60-
Logger.Info($"addGameDirPath:{path}");
61+
logger.Info($"addGameDirPath:{path}");
6162
}
62-
Logger.Info("--------------------------");
63+
logger.Info("--------------------------");
6364
}
6465
}
6566
}

MABProcessAtWait/Config.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ namespace MABProcessAtWait {
2020
public class Config {
2121
public static List<World> Configs = new List<World>();
2222
public static string ConfigPath = @".\Config\config.txt";
23+
public static Logger logger = new Logger("Config",".\\logs\\MABProcess.log");
2324

2425
/// <summary>
2526
/// ConfigファイルからAppに読み込む
2627
/// </summary>
2728
public static void Load() {
28-
Logger.Debug("call:LoadConfigToApp");
29+
logger.Debug("call:LoadConfigToApp");
2930
List<string> texts = new List<string>();
3031
using (StreamReader reader = new StreamReader(ConfigPath, Encoding.GetEncoding("utf-8"))) {
3132
while (reader.Peek() >= 0) {
3233
List<string> datas = reader.ReadLine().Split(',').ToList();
3334
datas = datas.Select(x => Util.TrimDoubleQuotationMarks(x)).ToList();
3435
Configs.Add(new World(datas[2], Convert.ToBoolean(datas[0]), Convert.ToBoolean(datas[4])));
3536
}
36-
Logger.Info($"Configから{Configs.Count()}件のワールドを読み込みました");
37+
logger.Info($"Configから{Configs.Count()}件のワールドを読み込みました");
3738
}
3839

3940
}
@@ -55,52 +56,52 @@ public static void Write() {
5556
/// </summary>
5657
public static List<World> SyncConfig() {
5758
ConsoleConfig();
58-
Logger.Debug("call:reloadConfig");
59+
logger.Debug("call:reloadConfig");
5960
List<World> worldInHdd = GetWorldDataFromHDD();
6061
List<World> worldInConfig = Configs;
61-
Logger.Debug($"config: {worldInConfig.Count()}");
62-
Logger.Debug($"HDD : {worldInHdd.Count()}");
62+
logger.Debug($"config: {worldInConfig.Count()}");
63+
logger.Debug($"HDD : {worldInHdd.Count()}");
6364

6465
int i = 0;
6566
//configに存在しないpathをconfigに追加する
6667
foreach (World pc in worldInHdd) {
67-
Logger.Debug($"pc:{i}回目");
68+
logger.Debug($"pc:{i}回目");
6869
//dobackup以外を比較して判定
6970
if (!worldInConfig.Select(x => $"{x.WorldPath}_{x.WorldIsAlive}").ToList().Contains($"{pc.WorldPath}_{pc.WorldIsAlive}")) {
70-
Logger.Info($"ADD {pc.WorldName}");
71+
logger.Info($"ADD {pc.WorldName}");
7172
Configs.Add(pc);
7273
}
7374
i++;
7475
}
7576
List<World> removeWorlds = new List<World>();
76-
Logger.Debug($"config: {worldInConfig.Count()}");
77-
Logger.Debug($"HDD : {worldInHdd.Count()}");
77+
logger.Debug($"config: {worldInConfig.Count()}");
78+
logger.Debug($"HDD : {worldInHdd.Count()}");
7879

7980
i = 0;
8081
int wI = 0;
81-
//Logger.Info("-----config一覧-----");
82+
//logger.Info("-----config一覧-----");
8283
//foreach(var a in worldInHdd.Select(x => new WorldForComparison(x)).ToList()) {
83-
// Logger.Info($"pc : {a.path}/{a.isAlive.ToString()}");
84+
// logger.Info($"pc : {a.path}/{a.isAlive.ToString()}");
8485
//}
85-
//Logger.Info("--------------------");
86+
//logger.Info("--------------------");
8687
foreach (World world in worldInConfig) {
87-
Logger.Debug($"config:{i}回目");
88+
logger.Debug($"config:{i}回目");
8889
//dobackup以外を比較して判定
8990
if (!worldInHdd.Select(x => $"{x.WorldPath}_{x.WorldIsAlive}").ToList().Contains($"{world.WorldPath}_{world.WorldIsAlive}")) {
9091
//config内のworldがHDDになかった場合
9192
if (GetBackups(world).Count() == 0) {
9293
// バックアップが一つもない場合はconfigから削除
93-
Logger.Info($"バックアップが一つもないのでRemoveWorldsに{world.WorldName}を追加");
94+
logger.Info($"バックアップが一つもないのでRemoveWorldsに{world.WorldName}を追加");
9495
removeWorlds.Add(world);
9596
}
9697
else {
9798
if (world.WorldIsAlive) {
9899
//バックアップが一つでもある場合は、backup一覧に表示するために殺すだけにする
99-
Logger.Info($"{world.WorldName}のバックアップが残っているため殺害");
100+
logger.Info($"{world.WorldName}のバックアップが残っているため殺害");
100101
Config.Configs[wI].WorldIsAlive = false;
101102
int count = 1;
102103
while (Directory.Exists($"{AppConfig.BackupPath}\\{Config.Configs[wI].WorldDir}\\{Config.Configs[wI].WorldName}_(削除済み)_{count}")) {
103-
Logger.Info($" path[ {AppConfig.BackupPath}\\{Config.Configs[wI].WorldDir}\\{Config.Configs[wI].WorldName}_(削除済み)_{count} ]");
104+
logger.Info($" path[ {AppConfig.BackupPath}\\{Config.Configs[wI].WorldDir}\\{Config.Configs[wI].WorldName}_(削除済み)_{count} ]");
104105
count++;
105106
}
106107

@@ -115,22 +116,22 @@ public static List<World> SyncConfig() {
115116
i++;
116117
}
117118

118-
Logger.Debug($"config: {worldInConfig.Count()}");
119-
Logger.Debug($"HDD : {worldInHdd.Count()}");
119+
logger.Debug($"config: {worldInConfig.Count()}");
120+
logger.Debug($"HDD : {worldInHdd.Count()}");
120121

121122
foreach (World w in removeWorlds) {
122123
if (Configs.Remove(w)) {
123-
Logger.Info($"REMOVE {w.WorldName} suc");
124+
logger.Info($"REMOVE {w.WorldName} suc");
124125
}
125126
else {
126-
Logger.Info($"REMOVE {w.WorldName} 見つかりませんでした");
127+
logger.Info($"REMOVE {w.WorldName} 見つかりませんでした");
127128
}
128129
}
129130

130131
Write();
131132

132-
Logger.Debug($"config: {worldInConfig.Count()}");
133-
Logger.Debug($"HDD : {worldInHdd.Count()}");
133+
logger.Debug($"config: {worldInConfig.Count()}");
134+
logger.Debug($"HDD : {worldInHdd.Count()}");
134135

135136
return removeWorlds;
136137
}
@@ -139,14 +140,14 @@ private static List<string> GetBackups(World w) {
139140
return Directory.GetDirectories(AppConfig.BackupPath + "\\" + w.WorldDir + "\\" + w.WorldName).ToList();
140141
}
141142
catch (DirectoryNotFoundException) {
142-
Logger.Info($"{AppConfig.BackupPath}\\{w.WorldDir}\\{w.WorldName} にアクセスできませんでした");
143+
logger.Info($"{AppConfig.BackupPath}\\{w.WorldDir}\\{w.WorldName} にアクセスできませんでした");
143144
return new List<string>();
144145
}
145146
}
146147

147148
public static void Change(string worldName, string worldDir, string doBackup) {
148-
Logger.Debug("call:Change");
149-
Logger.Debug("GET worldName: " + worldName + ", worldDir: " + worldDir + ", dobackup: " + doBackup);
149+
logger.Debug("call:Change");
150+
logger.Debug("GET worldName: " + worldName + ", worldDir: " + worldDir + ", dobackup: " + doBackup);
150151
List<World> _configs = new List<World>();
151152
foreach (World config in Configs) {
152153
if (config.WorldName == worldName && config.WorldDir == worldDir) {
@@ -166,7 +167,7 @@ public static void Change(string worldName, string worldDir, string doBackup) {
166167
/// </summary>
167168
/// <returns>取得したList(world)</returns>
168169
private static List<World> GetWorldDataFromHDD() {
169-
Logger.Debug("call:GetWorldDataFromPC");
170+
logger.Debug("call:GetWorldDataFromPC");
170171
List<World> worlds = new List<World>();
171172
List<string> _gameDirectory = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToList();
172173
List<string> gameDirectory = new List<string>();
@@ -193,7 +194,7 @@ private static List<World> GetWorldDataFromHDD() {
193194
/// <returns>取得したList(world)</returns>
194195
private static List<World> GetWorldDataFromHDD(List<string> gameDirectory) {
195196
List<World> worlds = new List<World>();
196-
Logger.Debug("call:GetWorldDataFromPC");
197+
logger.Debug("call:GetWorldDataFromPC");
197198
foreach (string dir in gameDirectory) {
198199
if (Directory.Exists($"{dir}\\saves")) {
199200
List<string> _worlds = Directory.GetDirectories($"{dir}\\saves").ToList();
@@ -206,11 +207,11 @@ private static List<World> GetWorldDataFromHDD(List<string> gameDirectory) {
206207
}
207208

208209
public static void ConsoleConfig() {
209-
Logger.Info("----Configs----");
210+
logger.Info("----Configs----");
210211
foreach (World w in Configs) {
211-
Logger.Info($"[{w.WorldDoBackup},{w.WorldName},{w.WorldPath},{w.WorldDir},]");
212+
logger.Info($"[{w.WorldDoBackup},{w.WorldName},{w.WorldPath},{w.WorldDir},]");
212213
}
213-
Logger.Info("---------------");
214+
logger.Info("---------------");
214215
}
215216
}
216217
}

MABProcessAtWait/Logger.cs

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,102 @@
33
using System.IO;
44
using System.Linq;
55

6-
namespace MABProcessAtWait {
7-
class Logger {
8-
public static string logPath = ".\\logs\\MABProcess.txt";
9-
private static int outputLevel = 2;
10-
11-
private static void Base(string logLevelStr, string message) {
12-
LogRotate();
13-
string logMessage = $"{DateTime.Now.ToString($"yyyy/MM/dd-HH:mm:ss")} [{logLevelStr}]:{message}\n";
14-
Console.WriteLine(logMessage);
15-
File.AppendAllText(logPath, logMessage);
16-
17-
}
18-
public static void Debug(string message) {
19-
if (outputLevel >= 3) {
20-
Base("DEBUG", message);
21-
}
6+
public class Logger {
7+
private const int DEFAULT_OUTPUT_LEVEL = 3;
8+
private const int MAX_LOG_COUNT = 500;
9+
private const string LOG_OUTPUT_PATH = ".\\logs";
10+
private string logPath;
11+
private int outputLevel;
12+
private string subProcessName;
13+
14+
public Logger(string subProcessName, string logPath, int outputLevel) {
15+
this.subProcessName = subProcessName;
16+
this.logPath = logPath;
17+
this.outputLevel = outputLevel;
18+
LogRotate();
19+
}
20+
21+
public Logger(string subProcessName, int outputLevel) {
22+
this.subProcessName = subProcessName;
23+
this.logPath = $".\\logs\\{subProcessName}.log";
24+
this.outputLevel = outputLevel;
25+
if (Directory.Exists(LOG_OUTPUT_PATH) == false) {
26+
Directory.CreateDirectory(LOG_OUTPUT_PATH);
2227
}
23-
public static void Info(string message) {
24-
if (outputLevel >= 2) {
25-
Base("INFO ", message);
26-
}
28+
LogRotate();
29+
}
30+
public Logger(string subProcessName, string logPath) {
31+
this.subProcessName = subProcessName;
32+
this.logPath = $".\\logs\\{subProcessName}.log";
33+
this.outputLevel = DEFAULT_OUTPUT_LEVEL;
34+
if (Directory.Exists(LOG_OUTPUT_PATH) == false) {
35+
Directory.CreateDirectory(LOG_OUTPUT_PATH);
2736
}
28-
public static void Warn(string message) {
29-
if (outputLevel >= 1) {
30-
Base("WARN ", message);
31-
}
37+
LogRotate();
38+
}
39+
40+
public Logger(string subProcessName) {
41+
this.subProcessName = subProcessName;
42+
this.logPath = $".\\logs\\{subProcessName}.log";
43+
this.outputLevel = DEFAULT_OUTPUT_LEVEL;
44+
if (Directory.Exists(LOG_OUTPUT_PATH) == false) {
45+
Directory.CreateDirectory(LOG_OUTPUT_PATH);
3246
}
33-
public static void Error(string message) {
34-
if (outputLevel >= 0) {
35-
Base("ERROR", message);
36-
}
47+
LogRotate();
48+
}
49+
50+
private void Base(string logLevelStr, string message) {
51+
string logMessage = $"{DateTime.Now.ToString($"yyyy/MM/dd-HH:mm:ss")} [{logLevelStr}]:[{subProcessName}]{message}\n";
52+
Console.WriteLine(logMessage);
53+
File.AppendAllText(logPath, logMessage);
54+
}
55+
public void Debug(string message) {
56+
if (outputLevel >= 3) {
57+
Base("DEBUG", message);
3758
}
59+
}
60+
public void Info(string message) {
61+
if (outputLevel >= 2) {
62+
Base("INFO ", message);
63+
}
64+
}
65+
public void Warn(string message) {
66+
if (outputLevel >= 1) {
67+
Base("WARN ", message);
3868

39-
public static List<string> GetLogFromFile() {
40-
List<string> logs = new List<string>();
69+
}
70+
}
71+
public void Error(string message) {
72+
if (outputLevel >= 0) {
73+
Base("ERROR", message);
74+
75+
}
76+
}
4177

42-
using (StreamReader s = new StreamReader(Logger.logPath)) {
78+
public List<string> GetLogFromFile() {
79+
List<string> logs = new List<string>();
80+
81+
try {
82+
using (StreamReader s = new StreamReader(this.logPath)) {
4383
string _logs = s.ReadToEnd();
4484
logs = _logs.Split('\n').ToList();
4585
}
46-
logs.Reverse();
47-
return logs;
48-
}
49-
public static string GetNearestLogFromFile() {
50-
return GetLogFromFile()[GetLogFromFile().Count - 2];
51-
}
52-
private static void LogRotate() {
53-
List<string> logs = GetLogFromFile();
54-
if (logs.Count >= 1000) {
55-
Console.WriteLine($"LogRotate : { logs[logs.Count - 1]} , {logs[logs.Count - 2]}を削除します");
56-
logs.Remove(logs[logs.Count - 1]);
57-
logs.Remove(logs[logs.Count - 2]);
58-
}
59-
logs.Reverse();
60-
File.WriteAllText(logPath, string.Join("\n", logs.ToArray()));
6186
}
87+
catch (FileNotFoundException) {
88+
File.Create(this.logPath).Close();
89+
}
90+
//logs.Reverse();
91+
return logs;
92+
}
93+
94+
private void LogRotate() {
95+
Info("LogRotate");
96+
List<string> logs = GetLogFromFile();
97+
//log総量が1000行を超えている場合は100行削除
98+
while (logs.Count >= MAX_LOG_COUNT) {
99+
logs.RemoveAt(0);
100+
}
101+
//logs.Reverse();
102+
File.WriteAllText(logPath, string.Join("\n", logs.ToArray()));
62103
}
63-
}
104+
}

0 commit comments

Comments
 (0)