aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/progress.cpp
blob: 01f6529f2c0e99eb0b83ccf3c0f82167eee95c5e (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenutil/progress.h>

#include <zencore/logging.h>

namespace zen {

class StandardProgressBase;

class StandardProgressBar : public ProgressBase::ProgressBar
{
public:
	StandardProgressBar(StandardProgressBase& Owner, std::string_view InSubTask) : m_Owner(Owner), m_SubTask(InSubTask) {}

	virtual void UpdateState(const State& NewState, bool DoLinebreak) override;
	virtual void ForceLinebreak() override {}
	virtual void Finish() override;

private:
	LoggerRef			  Log();
	StandardProgressBase& m_Owner;
	std::string			  m_SubTask;
	State				  m_State;
};

class StandardProgressBase : public ProgressBase
{
public:
	StandardProgressBase(LoggerRef Log) : m_Log(Log) {}

	virtual void SetLogOperationName(std::string_view Name) override
	{
		m_LogOperationName = Name;
		ZEN_INFO("{}", m_LogOperationName);
	}
	virtual void SetLogOperationProgress(uint32_t StepIndex, uint32_t StepCount) override
	{
		const size_t PercentDone = StepCount > 0u ? (100u * StepIndex) / StepCount : 0u;
		ZEN_INFO("{}: {}%", m_LogOperationName, PercentDone);
	}
	virtual void						 PushLogOperation(std::string_view Name) override { ZEN_UNUSED(Name); }
	virtual void						 PopLogOperation() override {}
	virtual uint32_t					 GetProgressUpdateDelayMS() const override { return 2000; }
	virtual std::unique_ptr<ProgressBar> CreateProgressBar(std::string_view InSubTask) override
	{
		return std::make_unique<StandardProgressBar>(*this, InSubTask);
	}

private:
	friend class StandardProgressBar;
	LoggerRef	m_Log;
	std::string m_LogOperationName;
	LoggerRef	Log() { return m_Log; }
};

LoggerRef
StandardProgressBar::Log()
{
	return m_Owner.Log();
}

void
StandardProgressBar::UpdateState(const State& NewState, bool DoLinebreak)
{
	ZEN_UNUSED(DoLinebreak);
	const size_t PercentDone =
		NewState.TotalCount > 0u ? (100u * (NewState.TotalCount - NewState.RemainingCount)) / NewState.TotalCount : 0u;
	std::string Task = NewState.Task;
	switch (NewState.Status)
	{
		case State::EStatus::Aborted:
			Task = "Aborting";
			break;
		case State::EStatus::Paused:
			Task = "Paused";
			break;
		default:
			break;
	}
	ZEN_INFO("{}: {}%{}", Task, PercentDone, NewState.Details.empty() ? "" : fmt::format(" {}", NewState.Details));
	m_State = NewState;
}
void
StandardProgressBar::Finish()
{
	if (m_State.RemainingCount > 0)
	{
		State NewState			= m_State;
		NewState.RemainingCount = 0;
		NewState.Details		= "";
		UpdateState(NewState, /*DoLinebreak*/ true);
	}
}

ProgressBase*
CreateStandardProgress(LoggerRef Log)
{
	return new StandardProgressBase(Log);
}

}  // namespace zen