• 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évisioncefcbc2e30803dd5933ae4555346e1247ec505a0 (tree)
l'heure2023-10-29 17:08:40
Auteurmio <stigma@disr...>
Commitermio

Message de Log

Replace global 'log' with stdThreadLocalLog

Change Summary

Modification

--- a/source/app.d
+++ b/source/app.d
@@ -144,8 +144,6 @@ enum ExitCode
144144 unknownOptionError = 3,
145145 }
146146
147-FileLogger log;
148-
149147 version(appimage) {
150148 enum ProgramName = "pixiv_down";
151149 }
@@ -308,6 +306,7 @@ void downloadIllust(ref Illustration illust, ref Config conf,
308306 {
309307 import std.algorithm.searching : canFind;
310308 import std.array : join, split;
309+ import std.experimental.logger : tracef, warningf;
311310 import std.file : FileException, mkdirRecurse, exists;
312311 import std.path : buildPath;
313312 import std.stdio : stderr;
@@ -321,7 +320,7 @@ void downloadIllust(ref Illustration illust, ref Config conf,
321320
322321 stderr.writefln("[%s] Downloading %s - %s", illust.type, illust.userName,
323322 illust.title);
324- log.tracef("Downloading post type %s", illust.type);
323+ tracef("Post Type = %s", illust.type);
325324
326325 scope monitor = new ProgressMonitor(illust.pages);
327326 conf.client.connect(&monitor.alreadyComplete);
@@ -331,8 +330,8 @@ void downloadIllust(ref Illustration illust, ref Config conf,
331330 try {
332331 conf.client.downloadIllust(illust, subDirectory, overwrite);
333332 } catch (FileException fe) {
334- log.warningf("FileException: %s (overwrite = %s)", fe.msg,
335- overwrite ? "true" : "false");
333+ warningf("FileException: %s (overwrite = %s)", fe.msg,
334+ overwrite ? "true" : "false");
336335 stderr.write("\r");
337336 stderr.writefln("[%s] %s - %s has already been downloaded", illust.type,
338337 illust.userName, illust.title);
@@ -344,15 +343,14 @@ void downloadIllust(ref Illustration illust, ref Config conf,
344343 void downloadArtist(string id, ContentType type, int offset,
345344 ref Config config)
346345 {
346+ import std.experimental.logger : infof, errorf;
347347 import std.net.curl : CurlException;
348348 import std.stdio : stderr;
349349
350350 UserBrief up = config.client.fetchUserAll(id);
351351
352352 if (ContentType.illustration == type) {
353-
354- log.infof("Total number of illustrations to download: %d",
355- up.illusts.length);
353+ infof("Number of Illustrations to download = %d", up.illusts.length);
356354
357355 foreach (elemNum, illustId; up.illusts) {
358356 Illustration illust = config.client.fetchIllustration(illustId);
@@ -360,7 +358,7 @@ void downloadArtist(string id, ContentType type, int offset,
360358 try {
361359 downloadIllust(illust, config);
362360 } catch (CurlException ce) {
363- log.warningf("CurlException: %s", ce.msg);
361+ errorf("CurlException: %s", ce.msg);
364362 stderr.writefln("ERROR: Failed to download illust");
365363 stderr.writefln("TRYING AGAIN");
366364 sleep(20, 30);
@@ -373,8 +371,7 @@ void downloadArtist(string id, ContentType type, int offset,
373371 sleep(4, 10);
374372 }
375373 } else if (ContentType.manga == type) {
376-
377- log.infof("Total number of Manga to download: %d", up.manga.length);
374+ infof("Number of Manga to download: %d", up.manga.length);
378375
379376 foreach (elemNum, illustId; up.manga) {
380377 Illustration illust = config.client.fetchIllustration(illustId);
@@ -382,7 +379,7 @@ void downloadArtist(string id, ContentType type, int offset,
382379 try {
383380 downloadIllust(illust, config);
384381 } catch (CurlException ce) {
385- log.warningf("CurlException: %s", ce.msg);
382+ errorf("CurlException: %s", ce.msg);
386383 stderr.writefln("ERROR: Failed to download manga.");
387384 stderr.writefln("TRYING AGAIN");
388385 sleep(20, 30);
@@ -405,6 +402,8 @@ void downloadArtist(string id, ContentType type, int offset,
405402 void downloadDaily(const ref SysTime begin, const ref SysTime end,
406403 string restrict, ref Config conf)
407404 {
405+ import std.experimental.logger : info, tracef;
406+
408407 Thumbnail[] dailyIllusts = conf.client.fetchIllustrationsFollowing();
409408
410409 bool reachedEndDate = false;
@@ -422,7 +421,7 @@ lDownload:
422421 /* Only download posts uploaded before or on `end`. */
423422 if (end > createDate) {
424423 reachedEndDate = true;
425- log.info("reachedEndDate = true");
424+ info("reachedEndDate = true");
426425 break;
427426 }
428427
@@ -431,13 +430,13 @@ lDownload:
431430 }
432431
433432 if (false == reachedEndDate) {
434- log.logf("finished downloading page %d", page);
435- log.info("reachedEndDate = false");
436- sleep(15, 20);
437- page += 1;
438- log.infof("will now download page %d", page);
439- dailyIllusts = conf.client.fetchIllustrationsFollowing(page);
440- goto lDownload;
433+ tracef("Finished downloading page %d", page);
434+ info("reachedEndDate = false");
435+ sleep(15, 20);
436+ page += 1;
437+ tracef("Proceeding to download page %d", page);
438+ dailyIllusts = conf.client.fetchIllustrationsFollowing(page);
439+ goto lDownload;
441440 }
442441 }
443442
@@ -452,6 +451,7 @@ lDownload:
452451 int artistHandle(string[] args, ref Config config)
453452 {
454453 import std.algorithm.searching : all;
454+ import std.experimental.logger : errorf;
455455 import std.stdio : stderr;
456456
457457 string enteredType;
@@ -486,7 +486,7 @@ int artistHandle(string[] args, ref Config config)
486486 if (all!"(a >= '0' && a <= '9')"(arg)) {
487487 ids ~= arg;
488488 } else {
489- log.errorf(`Invalid artist ID "%s"`, args);
489+ errorf(`Invalid artist ID "%s"`, args);
490490
491491 stderr.writefln("%s: Invalid artist ID provided.", args[0]);
492492 stderr.writeln("Artist IDs are required to be numbers.");
@@ -532,6 +532,7 @@ int artistHandle(string[] args, ref Config config)
532532 int artworkHandle(string[] args, ref Config config)
533533 {
534534 import std.conv : ConvException, to;
535+ import std.experimental.logger : info, errorf;
535536 import std.stdio : stderr;
536537
537538 // When an exception occurrs.
@@ -556,14 +557,14 @@ int artworkHandle(string[] args, ref Config config)
556557 }
557558 if ("--group-errors" == arg) {
558559 groupErrors = true;
559- log.info("errors will be grouped");
560+ info("errors will be grouped");
560561 continue;
561562 }
562563
563564 try {
564565 id = to!size_t(arg);
565566 } catch (ConvException ce) {
566- log.errorf("Could not convert %s to size_t", arg);
567+ errorf("Could not convert %s to size_t", arg);
567568
568569 if (groupErrors) {
569570 invalidArtworks ~= arg;
@@ -611,6 +612,7 @@ int dailyHandle(string[] args, ref Config config)
611612 import core.time : TimeException;
612613 import std.algorithm.searching : canFind;
613614 import std.datetime.systime : Clock;
615+ import std.experimental.logger : infof;
614616 import std.stdio : stderr;
615617
616618 string restrict = "both";
@@ -682,8 +684,7 @@ int dailyHandle(string[] args, ref Config config)
682684 return 1;
683685 }
684686
685- log.infof(`PARAMS=(restrict=%s rawBeginDate=%s rawEndDate=%s)`, restrict,
686- rawBeginDate, rawEndDate);
687+ infof("rawBeginDate=%s rawEndDate=%s", rawBeginDate, rawEndDate);
687688
688689 beginDate = Clock.currTime;
689690
@@ -742,6 +743,7 @@ int followingHandle(string[] args, ref Config config)
742743 {
743744 import std.container : SList;
744745 import std.conv : to;
746+ import std.experimental.logger : infof, tracef;
745747 import std.stdio : stderr, writefln;
746748
747749 bool pub = false;
@@ -813,7 +815,7 @@ int followingHandle(string[] args, ref Config config)
813815 // Web API uses "hide" and "show"
814816 string rest = prv ? "hide" : "show";
815817
816- log.tracef("rest = %s, skip = %d", rest, skip);
818+ infof("rest=%s skip=%d", rest, skip);
817819
818820 // Fetch all of the accounts to download.
819821 //
@@ -826,7 +828,7 @@ int followingHandle(string[] args, ref Config config)
826828 writefln("[following]: Downloading API JSON for page %d", pageCount);
827829 User[] users = config.client.fetchFollowing(offset, totalNumberOfAccounts,
828830 pageLimit, rest);
829- log.tracef("page %d returned %d users.", pageCount, users.length);
831+ infof("Page %d returned %d users.", pageCount, users.length);
830832 offset += users.length;
831833
832834 auto allUsers = SList!User();
@@ -834,14 +836,14 @@ int followingHandle(string[] args, ref Config config)
834836 while (offset < totalNumberOfAccounts) {
835837 writefln("[following]: Downloading API JSON for page %d", ++pageCount);
836838 users = config.client.fetchFollowing(offset, pageLimit, rest);
837- log.tracef("page %d returned %d users.", pageCount, users.length);
839+ infof("Page %d returned %d users.", pageCount, users.length);
838840 allUsers.insertAfter(allUsers[], users);
839841
840842 // Increase offset for next page fetch
841843 offset += users.length;
842844 }
843845
844- log.infof("totalNumberOfAccounts = %d", totalNumberOfAccounts);
846+ infof("totalNumberOfAccounts = %d", totalNumberOfAccounts);
845847
846848 size_t artistIndex = skip + 1;
847849 foreach(User user; allUsers) {
@@ -853,7 +855,7 @@ int followingHandle(string[] args, ref Config config)
853855
854856 writefln("[following]: Downloading artist %u of %u", artistIndex,
855857 totalNumberOfAccounts);
856- log.tracef("user %u/%u (ID = %s)", artistIndex, totalNumberOfAccounts,
858+ tracef("user %u/%u (ID = %s)", artistIndex, totalNumberOfAccounts,
857859 user.userId);
858860 artistHandle([args[0], "artist", user.userId], config);
859861 artistIndex += 1;
@@ -1132,14 +1134,14 @@ version(appimage) {
11321134 void
11331135 logLibGMVersion()
11341136 {
1137+ import std.experimental.logger : infof;
11351138 import std.string : fromStringz;
11361139 import graphicsmagick_c.magick.version_ : GetMagickVersion,
11371140 MagickLibVersionText, MagickQuantumDepth;
11381141
1139- log.infof("GraphicsMagick Version D: %s",
1140- fromStringz(GetMagickVersion(null)));
1141- log.infof("GraphicsMagick Version S: %s", MagickLibVersionText);
1142- log.infof("GraphicsMagick Depth S: %s", MagickQuantumDepth);
1142+ infof("GraphicsMagick Version D: %s", fromStringz(GetMagickVersion(null)));
1143+ infof("GraphicsMagick Version S: %s", MagickLibVersionText);
1144+ infof("GraphicsMagick Depth S: %s", MagickQuantumDepth);
11431145 }
11441146
11451147 void
@@ -1164,6 +1166,7 @@ resetSessionID()
11641166 Config
11651167 loadConfig()
11661168 {
1169+ import std.experimental.logger : infof;
11671170 import std.file : exists, mkdirRecurse;
11681171 import std.path : buildPath;
11691172 import std.string : strip;
@@ -1178,8 +1181,7 @@ loadConfig()
11781181 ProjectDirectories dirs = getProjectDirectories(null, "YumeNeru Software",
11791182 "pixiv_down");
11801183 UserDirectories userDirs = getUserDirectories();
1181- log.infof("dirs.configDir = %s", dirs.configDir);
1182- log.infof("userDirs.pictureDir = %s", userDirs.pictureDir);
1184+ infof("Configuration Directory = %s", dirs.configDir);
11831185
11841186 if (exists(dirs.configDir)) {
11851187 string configFilePath = buildPath(dirs.configDir, "settings.conf");
@@ -1195,8 +1197,8 @@ loadConfig()
11951197 // Set default values
11961198 config.baseFolder = userDirs.pictureDir;
11971199 }
1198- log.infof("config.baseFolder = %s", config.baseFolder);
1199- log.infof("data directory = %s", dirs.dataDir);
1200+ infof("Output Directory = %s", config.baseFolder);
1201+ infof("Local Data Directory = %s", dirs.dataDir);
12001202
12011203 const idFilePath = buildPath(dirs.dataDir, ".phpsessid");
12021204
@@ -1234,6 +1236,8 @@ int
12341236 main(string[] args)
12351237 {
12361238 import std.stdio : stderr, writeln, writefln;
1239+ import std.experimental.logger : stdThreadLocalLog;
1240+ import logger;
12371241
12381242 version(appimage) {
12391243 args[0] = ProgramName;
@@ -1286,9 +1290,7 @@ version(appimage) {
12861290 return 2;
12871291 }
12881292
1289- log = new FileLogger("pixiv_down.log");
1290- // By default, FileLogger opens in append.
1291- log.file.reopen("pixiv_down.log", "w");
1293+ stdThreadLocalLog = new PixivLogger("pixiv_down.log");
12921294
12931295 version (GMagick_Dynamic) {
12941296 bool loadedGM = loadLibGM(args[0]);
--- /dev/null
+++ b/source/logger.d
@@ -0,0 +1,30 @@
1+/*
2+ * pixiv_down - CLI-based downloading tool for https://www.pixiv.net.
3+ * Copyright (C) 2023 Mio
4+ *
5+ * This program is free software: you can redistribute it and/or modify
6+ * it under the terms of the GNU General Public License as published by
7+ * the Free Software Foundation, version 3 of the License.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16+ */
17+module logger;
18+
19+import std.experimental.logger : FileLogger;
20+
21+public class PixivLogger : FileLogger
22+{
23+private:
24+
25+ public this(string logFile) @safe
26+ {
27+ super(logFile);
28+ this.file.reopen(logFile, "w+");
29+ }
30+}