File: progress.cpp

package info (click to toggle)
pilercr 1.06%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 844 kB
  • sloc: cpp: 14,339; makefile: 67; sh: 36
file content (128 lines) | stat: -rwxr-xr-x 2,534 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "pilercr.h"
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

static time_t g_StartTime = time(0);
static time_t g_StartTimeStep;
static char *g_Msg = strsave("");
static bool g_NL = false;
bool g_ShowProgress = true;

int GetElapsedSecs()
	{
	return (int) (time(0) - g_StartTime);
	}

void ShowProgress(bool Show)
	{
	g_ShowProgress = Show;
	}

static char *Resources()
	{
	static char Str[64];

	int ElapsedSecs = GetElapsedSecs();
	unsigned MemUseMB = (GetMemUseBytes() + 500000)/1000000;
	unsigned RAMMB = (unsigned) ((GetRAMSize() + 500000)/1000000);
	unsigned MemUsePct = (MemUseMB*100)/RAMMB;

	sprintf(Str, "%4d s  %6d Mb (%3d%%) ", ElapsedSecs, MemUseMB, MemUsePct);
	return Str;
	}

void ProgressStep(int StepIndex, int StepCount)
	{
	if (!g_ShowProgress)
		return;

	double Pct = (double) StepIndex*100.0 / (double) StepCount;

	fprintf(stderr, "\r%s %6.2f%%  %s", Resources(), Pct, g_Msg);
//	fprintf(stderr, "%s  %6.2f%%  %s Step %d of %d\n", Resources(), Pct, g_Msg, StepIndex, StepCount);
	g_NL = false;
	}

void ProgressStart(const char *Format, ...)
	{
	g_StartTimeStep = time(0);
	char Str[4096];
	va_list ArgList;
	va_start(ArgList, Format);
	vsprintf(Str, Format, ArgList);
	if (g_Msg != 0)
		free(g_Msg);
	g_Msg = strsave(Str);

	if (!g_ShowProgress)
		return;

	if (!g_NL)
		fprintf(stderr, "\n");
	ProgressStep(0, 1);
	}

void ProgressDone()
	{
	if (0 == g_Msg)
		return;

	Log("%s %s\n", Resources(), g_Msg);

	if (g_ShowProgress)
		{
		fprintf(stderr, "\r%s %6.2f%%  %s\n", Resources(), 100.0, g_Msg);
		g_NL = true;
		}
	free(g_Msg);
	g_Msg = 0;
	}

void Progress(const char *Format, ...)
	{
	if (!g_ShowProgress)
		return;

	char Str[4096];
	va_list ArgList;
	va_start(ArgList, Format);
	vsprintf(Str, Format, ArgList);
	Log("%s %s\n", Resources(), Str);

	if (g_ShowProgress)
		{
		if (!g_NL)
			fprintf(stderr, "\n");
		fprintf(stderr, "%s %s\n", Resources(), Str);
		g_NL = true;
		}
	}

void ProgressNoRes(const char *Format, ...)
	{
	if (!g_ShowProgress)
		return;

	char Str[4096];
	va_list ArgList;
	va_start(ArgList, Format);
	vsprintf(Str, Format, ArgList);

	Log("%s\n", Str);

	if (g_ShowProgress)
		{
		if (!g_NL)
			fprintf(stderr, "\n");
		fprintf(stderr, "%s\n", Str);
		g_NL = true;
		}
	}

void ProgressExit()
	{
	time_t t = time(0);
	Log("%s", asctime(localtime(&t)));
	Progress("Finished, total elapsed time %ld secs.", GetElapsedSecs());
	}