How can I get the application name?

Hello,

I have a global function that I use in my applications and libraries to output error messages to cerr:

void sys_err(string str)
{
std::cerr << localtime() + str + " errno: " + strerror(errno) << endl << flush;
}

I need to add functionality to also output the name of the application.

I cannot use argv[0] because in some cases sys_err() used from the libraries so I do not have argv[0]. I know ps -ef gives the list of processes and corresponding commands so I guess this should be possible. I run Ubuntu.

Could someone help me with this please? -- THX!
Last edited on
On linux, you can get your process name from /proc/self/comm

as in:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
#include <string>
int main()
{
    std::ifstream comm("/proc/self/comm");
    std::string name;
    getline(comm, name);
    std::cout << "my name is " << name << '\n';
}
Last edited on
Thank you!
Topic archived. No new replies allowed.