libzypp  17.3.1
ProgressData.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/InputStream.h"
15 #include "zypp/base/String.h"
16 
17 #include "zypp/ProgressData.h"
18 
19 using std::endl;
20 
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "Progress"
23 
25 namespace zypp
26 {
27 
29  //
30  // METHOD NAME : ProgressData::report
31  // METHOD TYPE : void
32  //
34  {
35  Date now = Date::now();
36 
37  // compute value and check whether to report it
38  if ( hasRange() )
39  {
40  value_type newVal = _d->_val * 100 / ( _d->_max - _d->_min );
41 
42  if ( newVal - _d->_last_val > 10
43  || now - _d->_last_send > 1
44  || ( _d->_last_val == 0 && newVal > 0 )
45  || ( newVal == 100 && _d->_last_val != 100 )
46  || ( newVal != 100 && _d->_last_val == 100 )
47  || _d->_state != RUN /*INIT||END*/ )
48  {
49  _d->_last_val = newVal;
50  _d->_last_send = now;
51  }
52  else
53  return true; // skip report, continue per default
54  }
55  else
56  {
57  if ( now - _d->_last_send > 1 || _d->_state != RUN /*INIT||END*/ )
58  {
59  _d->_last_val = _d->_val;
60  _d->_last_send = now;
61  }
62  else
63  return true; // skip report, continue per default
64  }
65 
66  // now send report
67  if ( _d->_state == INIT )
68  {
69  _d->_state = RUN;
70  }
71  // XXX << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(), _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
72 
73  if ( _d->_receiver )
74  {
75  if ( ! _d->_receiver( *this ) )
76  {
77  if ( _d->_state != END )
78  {
79  WAR << "User request to ABORT pending action. "
80  << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(),
81  _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
82  }
83  return false; // aborted by user
84  }
85  }
86  else if ( _d->_state == END )
87  {
88  DBG << str::form( "{#%u|%s}END", numericId(), name().c_str() ) << endl;
89  }
90 
91  return true; // continue per default
92  }
93 
94  /******************************************************************
95  **
96  ** FUNCTION NAME : operator<<
97  ** FUNCTION TYPE : std::ostream &
98  */
99  std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
100  {
101  if ( obj.hasRange() )
102  {
103  return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
104  obj.numericId(), obj.name().c_str(),
105  obj.min(), obj.max(), obj.val(), obj.reportValue() );
106  }
107  return str << str::form( "{%u|%s}[-,-](%lld)",
108  obj.numericId(), obj.name().c_str(),
109  obj.val() );
110  }
111 
112  /******************************************************************
113  **
114  ** FUNCTION NAME : operator<<
115  ** FUNCTION TYPE : std::ostream &
116  */
118  {
119  ProgressData ret;
120  ret.name( input_r.name() );
121  if ( input_r.size() > 0 )
122  ret.range( input_r.size() );
123  return ret;
124  }
125 
127  ProgressData::value_type weight )
128  : _weight(weight),
129  _last_value(0),
130  _pd(pd)
131  {
132 
133  }
134 
136  {
137  if ( progress.reportAlive() || ( _weight == 0 ) )
138  return _pd.tick();
139 
140  // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
141  float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
142  // how much the subtask affects the parent task ie: 0,1
143  float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
144  // real increment of the parent task
145  float real_increment = parent_factor*increment;
146  _last_value = progress.val();
147  return _pd.incr( (int)( (_pd.max()-_pd.min()) * real_increment) );
148  }
149 
151 } // namespace zypp
ProgressData makeProgressData(const InputStream &input_r)
String related utilities and Regular expression matching.
Helper to create and pass std::istream.
Definition: InputStream.h:56
void min(value_type min_r)
Set new min value.
Definition: ProgressData.h:201
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:36
void max(value_type max_r)
Set new max value.
Definition: ProgressData.h:205
bool report()
Send report if necessary.
Definition: ProgressData.cc:33
std::streamoff size() const
Size of the input stream (informal).
Definition: InputStream.h:118
bool reportAlive() const
Definition: ProgressData.h:313
void range(value_type max_r)
Set new [0,max].
Definition: ProgressData.h:213
Store and operate on date (time_t).
Definition: Date.h:32
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
#define WAR
Definition: Logger.h:65
const std::string & name() const
Name of the std::istream.
Definition: InputStream.h:107
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:130
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: ProgressData.h:261
value_type val() const
Definition: ProgressData.h:295
const std::string & name() const
Definition: ProgressData.h:323
ProgressData::value_type _weight
Definition: ProgressData.h:417
static Date now()
Return the current time.
Definition: Date.h:78
value_type reportValue() const
Definition: ProgressData.h:319
bool tick()
Leave counter value unchanged (still alive).
Definition: ProgressData.h:277
long long value_type
Definition: ProgressData.h:133
bool hasRange() const
Definition: ProgressData.h:299
void name(const std::string &name_r)
Set counter name.
Definition: ProgressData.h:222
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
ProgressData::value_type _last_value
Definition: ProgressData.h:418
#define DBG
Definition: Logger.h:63
RWCOW_pointer< Data > _d
Pointer to data.
Definition: ProgressData.h:343