• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A CLI tool for downloading from pixiv.net


Commit MetaInfo

Révision7721ee127a50cfb8b6316270eb312d6d221e4444 (tree)
l'heure2023-12-10 14:54:59
Auteurmio <stigma@disr...>
Commitermio

Message de Log

Use custom logging for 'compact' command

Change Summary

Modification

--- a/source/cmds/compact.d
+++ b/source/cmds/compact.d
@@ -94,15 +94,34 @@ void compactAccounts(ref Config config, const ref Options options)
9494 import std.stdio : stderr;
9595 import std.random : Random, uniform, unpredictableSeed;
9696
97+ import pixivd.types : PixivJSONException;
98+ import logger;
99+
97100 auto duplicatedAccounts = findDuplicateDirectories(config.outputDirectory);
98101 duplicatedAccounts.each!((PairType pair) {
99- checkAndRemove(pair, config, options);
102+
103+ try {
104+ checkAndRemove(pair, config, options);
105+ } catch (PixivJSONException pje) {
106+ // TODO: Just because the user may have left, doesn't mean
107+ // that we can't condense their directories.
108+ stderr.writefln("ERROR: Failed to compact account for ID %s", pair[0]);
109+ stderr.writefln(" %s", pje.msg);
110+ log(LogLevel.ERROR, "checkAndRemove for ID %s: %s", pair[0],
111+ pje.msg);
112+ }
100113
101114 scope rnd = Random(unpredictableSeed);
102115 auto sleepDuration = uniform(3, 10, rnd);
103116 stderr.writefln("Sleeping for %d seconds...", sleepDuration);
104117 Thread.sleep(sleepDuration.seconds);
105118 });
119+
120+ if (options.dryRun)
121+ {
122+ import std.stdio : writeln;
123+ writeln(" No files or directories were moved.");
124+ }
106125 }
107126
108127 auto findDuplicateDirectories(string outputDirectory)
@@ -138,7 +157,7 @@ void checkAndRemove(PairType pair, ref Config config,
138157 import std.string : strip;
139158
140159 import mlib.trash : trash;
141-
160+ import logger;
142161 import util : makeSafe;
143162
144163 // Default to the current name
@@ -150,6 +169,8 @@ void checkAndRemove(PairType pair, ref Config config,
150169 newName = getChoice(newName, pair[1]);
151170 }
152171
172+ log(LogLevel.INFO, "newName = %s", newName);
173+
153174 const newDirName = buildPath(config.outputDirectory,
154175 user.userId ~ "_" ~ newName);
155176
@@ -186,18 +207,16 @@ void checkAndRemove(PairType pair, ref Config config,
186207
187208 if (false == options.dryRun) {
188209 trash(oldDirName);
210+ log(LogLevel.INFO, "Trashed %s", oldDirName);
211+
212+ writefln("Moved all files and directories:\n\tFrom: %s\n\tTo: %s",
213+ oldDirName, newDirName);
189214 } else {
190215 import std.range : repeat;
191216 import util : getTerminalColumns;
192217 writefln(" %s", '-'.repeat(getTerminalColumns() - 4));
193218 }
194219 }
195-
196- // TODO: Access to logging functions
197- {
198- writefln("INFO: Completed : %s", user.userName);
199- writefln("INFO: Used : %s", newName);
200- }
201220 }
202221
203222
--- a/source/logger.d
+++ b/source/logger.d
@@ -28,3 +28,70 @@ private:
2828 this.file.reopen(logFile, "w+");
2929 }
3030 }
31+
32+// New API (will replace above class-based API)
33+private:
34+
35+immutable kLogFileName = "pixiv_down.log";
36+shared int gMinLogLevel = LogLevel.INFO;
37+
38+public enum LogLevel
39+{
40+ ALL,
41+ TRACE,
42+ DEBUG,
43+ INFO,
44+ WARNING,
45+ ERROR,
46+ FATAL
47+}
48+
49+public void setLogLevel(LogLevel level)
50+{
51+ gMinLogLevel = level;
52+}
53+
54+public void log(Args...)(LogLevel logLevel, const(char)[] msg, Args args)
55+{
56+ import std.stdio : File;
57+
58+ if (logLevel < gMinLogLevel)
59+ return;
60+
61+ // TODO: Need some way to reset the log file
62+ // -- could just be in main?
63+ File file = File(kLogFileName, "a+");
64+
65+ switch (logLevel)
66+ {
67+ case LogLevel.TRACE:
68+ file.write("TRACE: ");
69+ break;
70+ case LogLevel.DEBUG:
71+ file.write("DEBUG: ");
72+ break;
73+ case LogLevel.INFO:
74+ file.write("INFO: ");
75+ break;
76+ case LogLevel.WARNING:
77+ file.write("WARNING: ");
78+ break;
79+ case LogLevel.ERROR:
80+ file.write("ERROR: ");
81+ break;
82+ case LogLevel.FATAL:
83+ file.write("FATAL: ");
84+ break;
85+ default:
86+ break;
87+ }
88+
89+ file.writefln(msg, args);
90+
91+ if (logLevel == LogLevel.FATAL)
92+ {
93+ import core.stdc.stdlib : EXIT_FAILURE, exit;
94+ exit(EXIT_FAILURE);
95+ }
96+}
97+