1 /* This file is part of pr-downloader (GPL v2 or later), see the LICENSE file */
2
3 #include "Logger.h"
4 #include "Util.h"
5
6 #include <stdio.h>
7 #include <stdarg.h>
8
9
L_LOG(L_LEVEL level,const char * format...)10 void L_LOG(L_LEVEL level, const char* format ...)
11 {
12 va_list args;
13 va_start(args,format);
14 switch(level) {
15 case L_RAW:
16 vprintf(format, args);
17 fflush(stdout);
18 break;
19 default:
20 case L_ERROR:
21 fprintf(stderr, "[Error] ");
22 vfprintf(stderr, format,args);
23 fprintf(stderr, "\n");
24 break;
25 case L_INFO:
26 printf("[Info] ");
27 vprintf(format, args);
28 printf("\n");
29 break;
30 case L_DEBUG:
31 printf("[Debug] ");
32 vprintf(format, args);
33 printf("\n");
34 break;
35 }
36 va_end(args);
37 }
38
LOG_DOWNLOAD(const char * filename)39 void LOG_DOWNLOAD(const char* filename)
40 {
41 L_LOG(L_RAW, "[Download] %s\n",filename);
42 }
43
44 static unsigned long lastlogtime=0;
45
LOG_PROGRESS(long done,long total,bool forceOutput)46 void LOG_PROGRESS(long done, long total, bool forceOutput)
47 {
48 unsigned long now=getTime();
49 if (lastlogtime<now) {
50 lastlogtime=now;
51 } else {
52 if (!forceOutput)
53 return;
54 }
55 if (total<0) //if total bytes are unknown set to 50%
56 total=done*2;
57 float percentage = 0;
58 if (total>0) {
59 percentage = (float)done / total;
60 }
61 L_LOG(L_RAW, "[Progress] %3.0f%% [",percentage * 100.0f);
62 int totaldotz = 30; // how wide you want the progress meter to be
63 int dotz = percentage * totaldotz;
64 for (int i=0; i < totaldotz; i++) {
65 if (i>=dotz)
66 printf(" "); //blank
67 else
68 printf("="); //full
69 }
70 // and back to line begin - do not forget the fflush to avoid output buffering problems!
71 L_LOG(L_RAW,"] %ld/%ld ", done, total);
72 L_LOG(L_RAW,"\r");
73 }
74
75