libzypp  17.31.0
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-core/base/InputStream>
15 #include <zypp/base/String.h>
16 
17 #include <zypp-core/ui/ProgressData>
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  DBG << str::form( "{#%u|%s} START", numericId(), name().c_str() ) << endl;
71  }
72  // XXX << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(), _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
73 
74  if ( _d->_receiver )
75  {
76  if ( ! _d->_receiver( *this ) )
77  {
78  if ( _d->_state != END )
79  {
80  WAR << "User request to ABORT pending action. "
81  << str::form( "{#%u|%s}(%lld%s)", numericId(), name().c_str(),
82  _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
83  }
84  return false; // aborted by user
85  }
86  }
87  else if ( _d->_state == END )
88  {
89  DBG << str::form( "{#%u|%s} END", numericId(), name().c_str() ) << endl;
90  }
91 
92  return true; // continue per default
93  }
94 
95  /******************************************************************
96  **
97  ** FUNCTION NAME : operator<<
98  ** FUNCTION TYPE : std::ostream &
99  */
100  std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
101  {
102  if ( obj.hasRange() )
103  {
104  return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
105  obj.numericId(), obj.name().c_str(),
106  obj.min(), obj.max(), obj.val(), obj.reportValue() );
107  }
108  return str << str::form( "{%u|%s}[-,-](%lld)",
109  obj.numericId(), obj.name().c_str(),
110  obj.val() );
111  }
112 
113  /******************************************************************
114  **
115  ** FUNCTION NAME : operator<<
116  ** FUNCTION TYPE : std::ostream &
117  */
119  {
120  ProgressData ret;
121  ret.name( input_r.name() );
122  if ( input_r.size() > 0 )
123  ret.range( input_r.size() );
124  return ret;
125  }
126 
128  ProgressData::value_type weight )
129  : _weight(weight),
130  _last_value(0),
131  _pd(pd)
132  {
133 
134  }
135 
137  {
138  if ( progress.reportAlive() || ( _weight == 0 ) )
139  return _pd.tick();
140 
141  // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
142  float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
143  // how much the subtask affects the parent task ie: 0,1
144  float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
145  // real increment of the parent task
146  float real_increment = parent_factor*increment;
147  _last_value = progress.val();
148  return _pd.incr( (int)( (_pd.max()-_pd.min()) * real_increment) );
149  }
150 
152 } // namespace zypp
ProgressData makeProgressData(const InputStream &input_r)
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
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
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
#define WAR
Definition: Logger.h:97
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:95
RWCOW_pointer< Data > _d
Pointer to data.
Definition: progressdata.h:343