Skip to content

Commit 8b43d26

Browse files
committed
string util + refresh context from specific pid
1 parent cc9cfec commit 8b43d26

1 file changed

Lines changed: 83 additions & 2 deletions

File tree

src/InfoLoggerContext.cxx

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ void InfoLoggerContext::reset() {
3838

3939
void InfoLoggerContext::refresh(){
4040

41+
// clear existing info
42+
reset();
43+
4144
// PID
42-
processId=-1;
4345
processId=(int)getpid();
4446

4547
// host name
46-
hostName.clear();
4748
char hostNameTmp[256]="";
4849
if (!gethostname(hostNameTmp,sizeof(hostNameTmp))) {
4950
// take short name only, not domain
@@ -53,6 +54,61 @@ void InfoLoggerContext::refresh(){
5354
hostName=hostNameTmp;
5455
}
5556

57+
// user name
58+
struct passwd *passent;
59+
passent = getpwuid(getuid());
60+
if(passent != NULL){
61+
userName=passent->pw_name;
62+
}
63+
64+
// read from runtime environment other fields
65+
// todo: or from other place...
66+
67+
const char* confEnv=nullptr;
68+
confEnv=getenv("O2_ROLE");
69+
if (confEnv!=NULL) {role=confEnv;}
70+
confEnv=getenv("O2_SYSTEM");
71+
if (confEnv!=NULL) {system=confEnv;}
72+
confEnv=getenv("O2_DETECTOR");
73+
if (confEnv!=NULL) {detector=confEnv;}
74+
confEnv=getenv("O2_PARTITION");
75+
if (confEnv!=NULL) {partition=confEnv;}
76+
confEnv=getenv("O2_RUN");
77+
if (confEnv!=NULL) {
78+
run=atoi(confEnv);
79+
if (run<=0) {
80+
run=-1;
81+
}
82+
}
83+
}
84+
85+
86+
87+
void InfoLoggerContext::refresh(pid_t pid){
88+
89+
// if zero, use current process info
90+
if (pid==0) {
91+
refresh();
92+
return;
93+
}
94+
95+
// clear existing info
96+
reset();
97+
98+
// PID
99+
processId=(int)pid;
100+
101+
// host name - this is local
102+
char hostNameTmp[256]="";
103+
if (!gethostname(hostNameTmp,sizeof(hostNameTmp))) {
104+
// take short name only, not domain
105+
char * dotptr;
106+
dotptr=strchr(hostNameTmp,'.');
107+
if (dotptr!=NULL) *dotptr=0;
108+
hostName=hostNameTmp;
109+
}
110+
111+
/*
56112
// user name
57113
userName.clear();
58114
struct passwd *passent;
@@ -80,9 +136,12 @@ void InfoLoggerContext::refresh(){
80136
run=-1;
81137
}
82138
}
139+
*/
83140
}
84141

85142

143+
144+
86145
int InfoLoggerContext::setField(FieldName key, const std::string &value){
87146
if (key==FieldName::Facility) {
88147
facility=value;
@@ -122,3 +181,25 @@ int InfoLoggerContext::setField(const std::list<std::pair<FieldName, const std::
122181
return numberOfErrors;
123182
}
124183

184+
185+
186+
int InfoLoggerContext::getFieldNameFromString(const std::string &input, FieldName &output) {
187+
const char *key=input.c_str();
188+
189+
if (!strcmp(key,"Facility")) {
190+
output=InfoLoggerContext::FieldName::Facility;
191+
} else if (!strcmp(key,"Role")) {
192+
output=InfoLoggerContext::FieldName::Role;
193+
} else if (!strcmp(key,"System")) {
194+
output=InfoLoggerContext::FieldName::System;
195+
} else if (!strcmp(key,"Detector")) {
196+
output=InfoLoggerContext::FieldName::Detector;
197+
} else if (!strcmp(key,"Partition")) {
198+
output=InfoLoggerContext::FieldName::Partition;
199+
} else if (!strcmp(key,"Run")) {
200+
output=InfoLoggerContext::FieldName::Run;
201+
} else {
202+
return -1;
203+
}
204+
return 0;
205+
}

0 commit comments

Comments
 (0)