Write log file to correct directory on Mac

* Move class to get the app data directory to a new StandardDirs class.
 * Setup an auto-release pool at the start/end of main() to catch objects
   allocated outside of the updater dialog.
This commit is contained in:
Robert Knight 2011-08-30 12:34:37 +01:00
parent ef4dc40b52
commit 50faf07f00
6 changed files with 114 additions and 59 deletions

View file

@ -3,69 +3,13 @@
#include "FileUtils.h"
#include "Platform.h"
#include "StringUtils.h"
#include "StandardDirs.h"
#include <iostream>
#ifdef PLATFORM_UNIX
#include <stdlib.h>
#include <pwd.h>
#endif
#ifdef PLATFORM_WINDOWS
#include <shlobj.h>
#endif
#ifdef PLATFORM_UNIX
std::string homeDir()
{
std::string dir = notNullString(getenv("HOME"));
if (!dir.empty())
{
return dir;
}
else
{
// note: if this process has been elevated with sudo,
// this will return the home directory of the root user
struct passwd* userData = getpwuid(getuid());
return notNullString(userData->pw_dir);
}
}
#endif
std::string appDataPath(const std::string& organizationName,
const std::string& appName)
{
#ifdef PLATFORM_LINUX
std::string xdgDataHome = notNullString(getenv("XDG_DATA_HOME"));
if (xdgDataHome.empty())
{
xdgDataHome = homeDir() + "/.local/share";
}
xdgDataHome += "/data/" + organizationName + '/' + appName;
return xdgDataHome;
#elif defined(PLATFORM_MAC)
// TODO - Mac implementation
#elif defined(PLATFORM_WINDOWS)
char buffer[MAX_PATH+1];
if (SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0 /* hToken */, SHGFP_TYPE_CURRENT, buffer) == S_OK)
{
std::string path = FileUtils::toUnixPathSeparators(notNullString(buffer));
path += '/' + organizationName + '/' + appName;
return path;
}
else
{
return std::string();
}
#endif
}
std::string AppInfo::logFilePath()
{
return appDataPath(organizationName(),appName()) + '/' + "update-log.txt";
return StandardDirs::appDataPath(organizationName(),appName()) + '/' + "update-log.txt";
}
std::string AppInfo::updateErrorMessage(const std::string& details)

View file

@ -29,13 +29,14 @@ set (SOURCES
FileUtils.cpp
Log.cpp
ProcessUtils.cpp
StandardDirs.cpp
UpdateInstaller.cpp
UpdateScript.cpp
UpdaterOptions.cpp
)
if (APPLE)
set(SOURCES ${SOURCES} UpdateDialogCocoa.mm)
set(SOURCES ${SOURCES} StandardDirs.mm UpdateDialogCocoa.mm)
endif()
if (WIN32)
set(SOURCES ${SOURCES} UpdateDialogWin32.cpp)
@ -47,6 +48,7 @@ set (HEADERS
FileUtils.h
Log.h
ProcessUtils.h
StandardDirs.h
UpdateInstaller.h
UpdateScript.h
UpdaterOptions.h

61
src/StandardDirs.cpp Normal file
View file

@ -0,0 +1,61 @@
#include "StandardDirs.h"
#include "StringUtils.h"
#ifdef PLATFORM_UNIX
#include <stdlib.h>
#include <pwd.h>
#endif
#ifdef PLATFORM_WINDOWS
#include <shlobj.h>
#endif
#ifdef PLATFORM_UNIX
std::string StandardDirs::homeDir()
{
std::string dir = notNullString(getenv("HOME"));
if (!dir.empty())
{
return dir;
}
else
{
// note: if this process has been elevated with sudo,
// this will return the home directory of the root user
struct passwd* userData = getpwuid(getuid());
return notNullString(userData->pw_dir);
}
}
#endif
std::string StandardDirs::appDataPath(const std::string& organizationName,
const std::string& appName)
{
#ifdef PLATFORM_LINUX
std::string xdgDataHome = notNullString(getenv("XDG_DATA_HOME"));
if (xdgDataHome.empty())
{
xdgDataHome = homeDir() + "/.local/share";
}
xdgDataHome += "/data/" + organizationName + '/' + appName;
return xdgDataHome;
#elif defined(PLATFORM_MAC)
std::string path = applicationSupportFolderPath();
path += '/' + appName;
return path;
#elif defined(PLATFORM_WINDOWS)
char buffer[MAX_PATH + 1];
if (SHGetFolderPath(0, CSIDL_LOCAL_APPDATA, 0 /* hToken */, SHGFP_TYPE_CURRENT, buffer) == S_OK)
{
std::string path = FileUtils::toUnixPathSeparators(notNullString(buffer));
path += '/' + organizationName + '/' + appName;
return path;
}
else
{
return std::string();
}
#endif
}

22
src/StandardDirs.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include "Platform.h"
#include <string>
class StandardDirs
{
public:
static std::string appDataPath(const std::string& organizationName,
const std::string& appName);
private:
#ifdef PLATFORM_UNIX
static std::string homeDir();
#endif
#ifdef PLATFORM_MAC
static std::string applicationSupportFolderPath();
#endif
};

18
src/StandardDirs.mm Normal file
View file

@ -0,0 +1,18 @@
#include <Foundation/Foundation.h>
#include "StandardDirs.h"
std::string StandardDirs::applicationSupportFolderPath()
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask,
true /* expand tildes */);
for (unsigned int i=0; i < [paths count]; i++)
{
NSString* path = [paths objectAtIndex:i];
return std::string([path UTF8String]);
}
return std::string();
}

View file

@ -52,6 +52,10 @@ void runUpdaterThread(void* arg)
int main(int argc, char** argv)
{
#ifdef PLATFORM_MAC
void* pool = UpdateDialogCocoa::createAutoreleasePool();
#endif
Log::instance()->open(AppInfo::logFilePath());
UpdaterOptions options;
options.parse(argc,argv);
@ -85,6 +89,10 @@ int main(int argc, char** argv)
installer.run();
}
#ifdef PLATFORM_MAC
UpdateDialogCocoa::releaseAutoreleasePool(pool);
#endif
return 0;
}