DWORD LWPrintException(FILE *dest, const LWException *print, BOOLEAN showTrace) { DWORD ceError = ERROR_SUCCESS; PSTR string = NULL; PSTR wrapped = NULL; int columns; ceError = LWExceptionToString(print, "Error: ", FALSE, showTrace, &string); GCE(ceError); //Don't word wrap if the terminal width can't be determined if (CTGetTerminalWidth(fileno(dest), &columns)) columns = -1; ceError = CTWordWrap(string, &wrapped, 4, columns); GCE(ceError); fprintf(dest, "%s\n", wrapped); cleanup: if(ceError) { fprintf(dest, "Error %x occurred while trying to print exception\n", ceError); } CT_SAFE_FREE_STRING(string); CT_SAFE_FREE_STRING(wrapped); return ceError; }
void PrintWarning(const JoinProcessOptions *options, const char *title, const char *message) { PSTR wrapped = NULL; int columns; if(CTGetTerminalWidth(fileno(stdout), &columns)) columns = -1; //This function doesn't return a DWORD, so we have to recover as much //as possible. if(!CTWordWrap(message, &wrapped, 4, columns)) fprintf(stdout, "Warning: %s\n%s\n\n", title, wrapped); else fprintf(stdout, "Warning: %s\n%s\n\n", title, message); CT_SAFE_FREE_STRING(wrapped); DJ_LOG_WARNING("%s\n%s", title, message); }
int main( int argc, char* argv[] ) { LWException *exc = NULL; int columns; PSTR pszLogFilePath = NULL; BOOLEAN bNoLog = FALSE; PSTR logLevel = "warning"; DWORD dwLogLevel; BOOLEAN showHelp = FALSE; BOOLEAN showInternalHelp = FALSE; int remainingArgs = argc; char **argPos = argv; int i; BOOLEAN directoryExists = FALSE; if(CTGetTerminalWidth(fileno(stdout), &columns)) columns = -1; /* Skip the program name */ argPos++; remainingArgs--; setlocale(LC_ALL, ""); while(remainingArgs > 0 && CTStrStartsWith(argPos[0], "--")) { if(!strcmp(argPos[0], "--help")) showHelp = TRUE; else if(!strcmp(argPos[0], "--help-internal")) showInternalHelp = TRUE; else if(!strcmp(argPos[0], "--nolog")) bNoLog = TRUE; //All options after this point take an argument else if(remainingArgs < 2) showHelp = TRUE; else if(!strcmp(argPos[0], "--logfile")) { pszLogFilePath = (++argPos)[0]; remainingArgs--; } else if(!strcmp(argPos[0], "--loglevel")) { logLevel = (++argPos)[0]; remainingArgs--; } else break; remainingArgs--; argPos++; } if(remainingArgs < 1) showHelp = TRUE; if (showInternalHelp) { ShowUsageInternal(); goto cleanup; } if (showHelp) { ShowUsage(); goto cleanup; } if (!strcasecmp(logLevel, "error")) { dwLogLevel = LOG_LEVEL_ERROR; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_ERROR); } else if (!strcasecmp(logLevel, "warning")) { dwLogLevel = LOG_LEVEL_WARNING; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_WARNING); } else if (!strcasecmp(logLevel, "info")) { dwLogLevel = LOG_LEVEL_INFO; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_INFO); } else if (!strcasecmp(logLevel, "verbose")) { dwLogLevel = LOG_LEVEL_VERBOSE; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_VERBOSE); } else if (!strcasecmp(logLevel, "debug")) { dwLogLevel = LOG_LEVEL_VERBOSE; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_DEBUG); } else { LW_CLEANUP_CTERR(&exc, LW_ERROR_INVALID_LOG_LEVEL); } if (pszLogFilePath == NULL) { // Determine the default log path LW_CLEANUP_CTERR(&exc, CTCheckDirectoryExists("/var/log", &directoryExists)); if (directoryExists) { pszLogFilePath = "/var/log/domainjoin-cli.log"; } else { pszLogFilePath = "/var/adm/domainjoin-cli.log"; } } if (bNoLog) { LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } else if (!strcmp(pszLogFilePath, ".")) { LW_CLEANUP_CTERR(&exc, dj_init_logging_to_console(dwLogLevel)); } else { DWORD ceError = dj_init_logging_to_file(dwLogLevel, pszLogFilePath); if(ceError == ERROR_ACCESS_DENIED) { fprintf(stderr, "Warning: insufficient permissions to log to %s. To enable logging, please specify a different filename with --logfile <file>.\n", pszLogFilePath); ceError = ERROR_SUCCESS; LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } else if (ceError == ERROR_FILE_NOT_FOUND) { fprintf(stderr, "Warning: parent directory of log file %s does not exist. To enable logging, please specify a different filename with --logfile <file>.\n", pszLogFilePath); ceError = ERROR_SUCCESS; LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } LW_CLEANUP_CTERR(&exc, ceError); } LwRtlLogSetCallback(RtlLogCallback, NULL); if (!strcmp(argPos[0], "join") || !strcmp(argPos[0], "leave")) { DJ_LOG_INFO("Domainjoin invoked with the %s command (remaining arguments will be printed later):", argPos[0]); // Only print up to the 'join' part for (i = 0; i <= argPos - argv; i++) { DJ_LOG_INFO(" [%s]", argv[i]); } } else { DJ_LOG_INFO("Domainjoin invoked with %d arg(s):", argc); for (i = 0; i < argc; i++) { DJ_LOG_INFO(" [%s]", argv[i]); } } if(!strcmp(argPos[0], "setname")) { PSTR pDomainSuffix = 0; argPos++; if(--remainingArgs != 1) { ShowUsage(); goto cleanup; } pDomainSuffix = strchr(argPos[0], '.'); if (pDomainSuffix) { *pDomainSuffix = 0; pDomainSuffix++; } else { pDomainSuffix = ""; } LW_TRY(&exc, DJSetComputerName(argPos[0], pDomainSuffix, &LW_EXC)); } else if(!strcmp(argPos[0], "join")) { argPos++; remainingArgs--; LW_TRY(&exc, DoJoin(remainingArgs, argPos, columns, &LW_EXC)); } else if(!strcmp(argPos[0], "leave")) { argPos++; remainingArgs--; LW_TRY(&exc, DoLeaveNew(remainingArgs, argPos, columns, &LW_EXC)); } else if(!strcmp(argPos[0], "query")) { LW_TRY(&exc, DoQuery(&LW_EXC)); } else if(!strcmp(argPos[0], "fixfqdn")) LW_TRY(&exc, DoFixFqdn(&LW_EXC)); #ifndef ENABLE_MINIMAL else if(!strcmp(argPos[0], "configure")) { argPos++; remainingArgs--; LW_TRY(&exc, DoConfigure(remainingArgs, argPos, &LW_EXC)); } #endif else if(!strcmp(argPos[0], "get_os_type") || !strcmp(argPos[0], "get_arch") || !strcmp(argPos[0], "get_distro") || !strcmp(argPos[0], "get_distro_version")) { LW_TRY(&exc, DoGetDistroInfo(remainingArgs, argPos, &LW_EXC)); } else { LW_RAISE(&exc, LW_ERROR_SHOW_USAGE); goto cleanup; } cleanup: if (!LW_IS_OK(exc) && exc->code == LW_ERROR_SHOW_USAGE) { ShowUsage(); LWHandle(&exc); } else if (!LW_IS_OK(exc)) { //Ignoring the return value from this because we can't do anything //if there is an error fprintf(stdout, "\n"); LWPrintException(stdout, exc, FALSE); DJLogException(LOG_LEVEL_ERROR, exc); LWHandle(&exc); dj_close_log(); return 1; } dj_close_log(); return 0; }