1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ProgressData.cc
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/InputStream.h"
15 #include "zypp/base/String.h"
17 #include "zypp/ProgressData.h"
21 #undef ZYPP_BASE_LOGGER_LOGGROUP
22 #define ZYPP_BASE_LOGGER_LOGGROUP "Progress"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 // METHOD NAME : ProgressData::report
33 bool ProgressData::report()
35 bool goOn = true; // continue per default
36 bool doReport = false;
38 // compute value and check whether to report it
41 value_type newVal = _d->_val * 100;
42 newVal /= ( _d->_max - _d->_min );
44 if ( newVal - _d->_last_val > 20
45 || Date::now() - _d->_last_send > 1
46 || ( newVal == 100 && _d->_last_send != 100 )
47 || _d->_state == END )
49 _d->_last_val = newVal;
50 _d->_last_send = Date::now();
56 if ( Date::now() - _d->_last_send > 1 || _d->_state == END )
58 _d->_last_val = _d->_val;
59 _d->_last_send = Date::now();
64 // report if necessary
67 if ( _d->_state == INIT )
74 goOn = _d->_receiver( *this );
78 if ( _d->_state != END )
80 XXX << str::form( "{#%u|%s}(%lld%s)",
81 numericId(), name().c_str(),
82 _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
86 DBG << str::form( "{#%u|%s}END", numericId(), name().c_str() ) << endl;
91 // log abort request and return
92 if ( ! goOn && _d->_state != END )
94 WAR << "User request to ABORT pending action. "
95 << str::form( "{#%u|%s}(%lld%s)",
96 numericId(), name().c_str(),
97 _d->_last_val, ( hasRange() ? "%" : "!" ) ) << endl;
102 /******************************************************************
104 ** FUNCTION NAME : operator<<
105 ** FUNCTION TYPE : std::ostream &
107 std::ostream & operator<<( std::ostream & str, const ProgressData & obj )
109 if ( obj.hasRange() )
111 return str << str::form( "{%u|%s}[%lld,%lld](%lld)%lld%%)",
112 obj.numericId(), obj.name().c_str(),
113 obj.min(), obj.max(), obj.val(), obj.reportValue() );
115 return str << str::form( "{%u|%s}[-,-](%lld)",
116 obj.numericId(), obj.name().c_str(),
120 /******************************************************************
122 ** FUNCTION NAME : operator<<
123 ** FUNCTION TYPE : std::ostream &
125 ProgressData makeProgressData( const InputStream & input_r )
128 ret.name( input_r.name() );
129 if ( input_r.size() > 0 )
130 ret.range( input_r.size() );
134 CombinedProgressData::CombinedProgressData( ProgressData &pd,
135 ProgressData::value_type weight )
143 bool CombinedProgressData::operator()( const ProgressData &progress )
145 if ( progress.reportAlive() || ( _weight == 0 ) )
148 // factor [0,1] of increase in subtask ( ie: before 0,2 now 0.5 )
149 float increment = ((float)(progress.val() - _last_value))/(progress.max() - progress.min());
150 // how much the subtask affects the parent task ie: 0,1
151 float parent_factor = (float)(_weight)/(_pd.max() - _pd.min());
152 // real increment of the parent task
153 float real_increment = parent_factor*increment;
154 _last_value = progress.val();
155 return _pd.incr( (int)( (_pd.max()-_pd.min()) * real_increment) );
158 /////////////////////////////////////////////////////////////////
160 ///////////////////////////////////////////////////////////////////