client-cpp  0.10.0
PeriodicLogUploadStrategy.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 PERIODICLOGUPLOADSTRATEGY_HPP_
19 #define PERIODICLOGUPLOADSTRATEGY_HPP_
20 
21 #include <cstdlib>
22 #include <chrono>
23 
24 #include "kaa/logging/Log.hpp"
27 
28 namespace kaa {
29 
31 public:
32  PeriodicLogUploadStrategy(std::size_t logUploadCheckReriod, IKaaClientContext &context)
33  : DefaultLogUploadStrategy(context), lastUploadTime_(Clock::now())
34  {
35  setLogUploadCheckPeriod(logUploadCheckReriod);
36  }
37 
39  {
40  auto now = Clock::now();
41 
42  if (now >= (lastUploadTime_ + std::chrono::seconds(logUploadCheckReriod_))) {
43  KAA_LOG_INFO(boost::format("Need to upload logs - current count: %llu, lastUploadedTime: %llu, timeLimit: %llu sec")
44  % status.getRecordsCount() % lastUploadTime_.time_since_epoch().count()
46  lastUploadTime_ = now;
48  }
49 
51  }
52 
53 private:
54  typedef std::chrono::system_clock Clock;
55  std::chrono::time_point<Clock> lastUploadTime_;
56 };
57 
58 }
59 
60 #endif /* PERIODICLOGUPLOADSTRATEGY_HPP_ */
PeriodicLogUploadStrategy(std::size_t logUploadCheckReriod, IKaaClientContext &context)
void setLogUploadCheckPeriod(std::size_t period)
The default ILogUploadStrategy implementation.
#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.
virtual std::size_t getRecordsCount()=0
Returns the number of collected logs.
The public interface to represent the current log storage state.