Move user name retrieval to a common API.

This commit is contained in:
Bartosz Taudul
2025-12-30 13:41:19 +01:00
parent 6a26472278
commit da85325686
3 changed files with 28 additions and 31 deletions

View File

@@ -595,39 +595,13 @@ static const char* GetHostInfo()
#if defined _WIN32
InitWinSock();
char hostname[512];
gethostname( hostname, 512 );
# if defined TRACY_WIN32_NO_DESKTOP
const char* user = "";
# else
DWORD userSz = UNLEN+1;
char user[UNLEN+1];
GetUserNameA( user, &userSz );
# endif
ptr += sprintf( ptr, "User: %s@%s\n", user, hostname );
#else
char hostname[_POSIX_HOST_NAME_MAX]{};
gethostname( hostname, _POSIX_HOST_NAME_MAX );
# if defined __ANDROID__
const auto login = getlogin();
if( login )
{
ptr += sprintf( ptr, "User: %s@%s\n", login, hostname );
}
else
{
ptr += sprintf( ptr, "User: (?)@%s\n", hostname );
}
# else
char user[_POSIX_LOGIN_NAME_MAX]{};
getlogin_r( user, _POSIX_LOGIN_NAME_MAX );
ptr += sprintf( ptr, "User: %s@%s\n", user, hostname );
# endif
#endif
const char* user = GetUserName();
char hostname[512] = {};
gethostname( hostname, sizeof( hostname ) );
ptr += sprintf( ptr, "User: %s@%s\n", user, hostname );
#if defined __i386 || defined _M_IX86
ptr += sprintf( ptr, "Arch: x86\n" );
#elif defined __x86_64__ || defined _M_X64

View File

@@ -336,6 +336,28 @@ TRACY_API const char* GetEnvVar( const char* name )
#endif
}
TRACY_API const char* GetUserName()
{
#if defined _WIN32
# if defined TRACY_WIN32_NO_DESKTOP
return "(?)";
# elif
DWORD userSz = UNLEN+1;
static char user[UNLEN+1];
GetUserNameA( user, &userSz );
return user;
# endif
#elif defined __ANDROID__
const auto user = getlogin();
if( user ) return user;
return "(?)";
#else
static char user[1024] = {};
getlogin_r( user, sizeof( user ) );
return user;
#endif
}
}
#ifdef __cplusplus

View File

@@ -37,6 +37,7 @@ TRACY_API void SetThreadNameWithHint( const char* name, int32_t groupHint );
TRACY_API const char* GetThreadName( uint32_t id );
TRACY_API const char* GetEnvVar( const char* name );
TRACY_API const char* GetUserName();
}