-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSampleLinuxDaemon.c
More file actions
executable file
·79 lines (64 loc) · 2.4 KB
/
SampleLinuxDaemon.c
File metadata and controls
executable file
·79 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/******************************************************************************
*
* Copyright (c) Phoenix Contact GmbH & Co. KG. All rights reserved.
* Licensed under the MIT. See LICENSE file in the project root for full license information.
* SPDX-License-Identifier: MIT
*
* SampleLinuxDaemon.cpp
*
******************************************************************************/
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <stdlib.h>
#include <unistd.h>
#include "cppformat/format.h"
const std::string appIdentifier = "SampleLinuxDaemonApp";
std::string GetCurrentTimeStamp()
{
std::string result = "";
auto now = std::chrono::system_clock::now();
time_t cnow = std::chrono::system_clock::to_time_t(now);
struct tm* tmDateTime = std::localtime(&cnow);
const char * timeFormatStr = "%02d%02d%02d-%02d%02d%02d";
result = fmt::sprintf(timeFormatStr,
(tmDateTime->tm_year + 1900),
(tmDateTime->tm_mon + 1),
tmDateTime->tm_mday,
tmDateTime->tm_hour,
tmDateTime->tm_min,
tmDateTime->tm_sec);
return result;
}
int main(void)
{
std::cout << "Starting Sample Linux Daemon" << std::endl;
char* plcnextAppsDataDir = getenv("APPDATADIRECTORY");
std::string appPersistentDataDir;
// Get Apps persistent Data directory
if(plcnextAppsDataDir == NULL)
{ // Variant 1 to get persistent data storage path (via app identifier)
std::cout << "Couldn't get persistent data dir env variable, using default path" << std::endl;
appPersistentDataDir = "/opt/plcnext/appshome/data/" + appIdentifier;
}
else
{
// Variant 2 to get to get persistent data storage path (via environment variable exported/passed in the init script)
appPersistentDataDir = std::string(plcnextAppsDataDir);
}
std::cout << " Daemon persistent Data dir = " << appPersistentDataDir << std::endl;
std::string logFile = appPersistentDataDir + "/linuxDaemonOut.log";
std::ofstream outfile(logFile, std::ofstream::out | std::ofstream::app);
std::cout << " Daemon log data file = " << logFile << std::endl;
unsigned int counterValue = 0;
while(1)
{
outfile << GetCurrentTimeStamp() << " - counter = " << counterValue++ << std::endl;
//std::cout << "Current Time Stamp = " << GetCurrentTimeStamp() << std::endl;
sleep(1);
}
outfile.close();
return 0;
}