40253f69a3c0d8251f5fab545f689602621d42f8
[platform/core/context/context-provider.git] / src / my-place / utils / debug_utils.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include "debug_utils.h"
18 #include <Types.h>
19 #include <iomanip>
20
21 /*
22  * Number of digits after decimal point used in geo coordinates.
23  */
24 #define GEO_LOCATION_PRECISION 7
25
26 std::string ctx::DebugUtils::humanReadableDateTime(time_t timestamp, std::string format, size_t size, bool utc)
27 {
28         struct tm timeinfo;
29         struct tm *result;
30         tzset();
31         if (utc) {
32                 format += " UTC";
33                 size += 4;
34                 result = gmtime_r(&timestamp, &timeinfo);
35         } else {
36                 result = localtime_r(&timestamp, &timeinfo);
37         }
38         char buffer[size];
39         if (result) {
40                 strftime(buffer, size, format.c_str(), &timeinfo);
41         } else {
42                 snprintf(buffer, size, "NULL");
43         }
44         return std::string(buffer);
45 }
46
47 void ctx::DebugUtils::printPlace2Stream(const Place &place, std::ostream &out)
48 {
49         out << "PLACE:" << std::endl;
50         out << "__CATEGORY: " << place.name << std::endl;
51         if (place.locationValid) {
52                 out << "__LOCATION: lat=" << std::setprecision(GEO_LOCATION_PRECISION + 2) << place.location.latitude;
53                 out << ", lon=" << place.location.longitude << std::setprecision(5) << std::endl;
54         }
55         out << "__WIFI:" << std::endl;
56         for (std::pair<std::string, std::string> ap : place.wifiAps) {
57                 out << "____ " << ap.first << " : " << ap.second << std::endl;
58         }
59         out << "__CREATE_DATE: " << humanReadableDateTime(place.createDate, "%F %T", 80) << std::endl;
60 }