client-cpp  0.10.0
StorageSizeWithTimeLimitLogUploadStrategy.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef STORAGESIZEWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_
19 #define STORAGESIZEWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_
20 
21 #include <cstdlib>
22 #include <chrono>
23 
24 #include "kaa/logging/Log.hpp"
27 
28 namespace kaa {
29 
31 public:
32  StorageSizeWithTimeLimitLogUploadStrategy(std::size_t volumeThreshold, std::size_t logUploadCheckReriod, IKaaClientContext &context)
33  : DefaultLogUploadStrategy(context), lastUploadTime_(Clock::now())
34  {
35  setVolumeThreshold(volumeThreshold);
36  setLogUploadCheckPeriod(logUploadCheckReriod);
37  }
38 
40  {
41  auto now = Clock::now();
42  auto currentConsumedVolume = status.getConsumedVolume();
43 
44  if ((currentConsumedVolume >= uploadVolumeThreshold_) || (now >= (lastUploadTime_ + std::chrono::seconds(logUploadCheckReriod_)))) {
45  KAA_LOG_INFO(boost::format("Need to upload logs - current size: %llu, threshold: %llu, lastUploadedTime: %llu, timeLimit: %llu sec")
46  % currentConsumedVolume % uploadVolumeThreshold_ % lastUploadTime_.time_since_epoch().count() % logUploadCheckReriod_);
47  lastUploadTime_ = now;
49  }
50 
52  }
53 
54 private:
55  typedef std::chrono::system_clock Clock;
56  std::chrono::time_point<Clock> lastUploadTime_;
57 };
58 
59 }
60 
61 #endif /* STORAGESIZEWITHTIMELIMITLOGUPLOADSTRATEGY_HPP_ */
virtual std::size_t getConsumedVolume()=0
Returns amount of bytes collected logs are consumed.
void setLogUploadCheckPeriod(std::size_t period)
The default ILogUploadStrategy implementation.
void setVolumeThreshold(std::size_t maxVolume)
#define KAA_LOG_INFO(message)
Definition: Log.hpp:64
virtual LogUploadStrategyDecision isUploadNeeded(ILogStorageStatus &status) override
Decides whether the log upload is needed.
LogUploadStrategyDecision
Log upload decisions.
StorageSizeWithTimeLimitLogUploadStrategy(std::size_t volumeThreshold, std::size_t logUploadCheckReriod, IKaaClientContext &context)
The public interface to represent the current log storage state.