A CLI tool for downloading from pixiv.net
Révision | 7721ee127a50cfb8b6316270eb312d6d221e4444 (tree) |
---|---|
l'heure | 2023-12-10 14:54:59 |
Auteur | mio <stigma@disr...> |
Commiter | mio |
Use custom logging for 'compact' command
@@ -94,15 +94,34 @@ void compactAccounts(ref Config config, const ref Options options) | ||
94 | 94 | import std.stdio : stderr; |
95 | 95 | import std.random : Random, uniform, unpredictableSeed; |
96 | 96 | |
97 | + import pixivd.types : PixivJSONException; | |
98 | + import logger; | |
99 | + | |
97 | 100 | auto duplicatedAccounts = findDuplicateDirectories(config.outputDirectory); |
98 | 101 | 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 | + } | |
100 | 113 | |
101 | 114 | scope rnd = Random(unpredictableSeed); |
102 | 115 | auto sleepDuration = uniform(3, 10, rnd); |
103 | 116 | stderr.writefln("Sleeping for %d seconds...", sleepDuration); |
104 | 117 | Thread.sleep(sleepDuration.seconds); |
105 | 118 | }); |
119 | + | |
120 | + if (options.dryRun) | |
121 | + { | |
122 | + import std.stdio : writeln; | |
123 | + writeln(" No files or directories were moved."); | |
124 | + } | |
106 | 125 | } |
107 | 126 | |
108 | 127 | auto findDuplicateDirectories(string outputDirectory) |
@@ -138,7 +157,7 @@ void checkAndRemove(PairType pair, ref Config config, | ||
138 | 157 | import std.string : strip; |
139 | 158 | |
140 | 159 | import mlib.trash : trash; |
141 | - | |
160 | + import logger; | |
142 | 161 | import util : makeSafe; |
143 | 162 | |
144 | 163 | // Default to the current name |
@@ -150,6 +169,8 @@ void checkAndRemove(PairType pair, ref Config config, | ||
150 | 169 | newName = getChoice(newName, pair[1]); |
151 | 170 | } |
152 | 171 | |
172 | + log(LogLevel.INFO, "newName = %s", newName); | |
173 | + | |
153 | 174 | const newDirName = buildPath(config.outputDirectory, |
154 | 175 | user.userId ~ "_" ~ newName); |
155 | 176 |
@@ -186,18 +207,16 @@ void checkAndRemove(PairType pair, ref Config config, | ||
186 | 207 | |
187 | 208 | if (false == options.dryRun) { |
188 | 209 | 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); | |
189 | 214 | } else { |
190 | 215 | import std.range : repeat; |
191 | 216 | import util : getTerminalColumns; |
192 | 217 | writefln(" %s", '-'.repeat(getTerminalColumns() - 4)); |
193 | 218 | } |
194 | 219 | } |
195 | - | |
196 | - // TODO: Access to logging functions | |
197 | - { | |
198 | - writefln("INFO: Completed : %s", user.userName); | |
199 | - writefln("INFO: Used : %s", newName); | |
200 | - } | |
201 | 220 | } |
202 | 221 | |
203 | 222 |
@@ -28,3 +28,70 @@ private: | ||
28 | 28 | this.file.reopen(logFile, "w+"); |
29 | 29 | } |
30 | 30 | } |
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 | + |