1 /* 2 * The Initial Developer of the Original Code is International 3 * Business Machines Corporation. Portions created by IBM 4 * Corporation are Copyright (C) 2005 International Business 5 * Machines Corporation. All Rights Reserved. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the Common Public License as published by 9 * IBM Corporation; either version 1 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * Common Public License for more details. 16 * 17 * You should have received a copy of the Common Public License 18 * along with this program; if not, a copy can be viewed at 19 * http://www.opensource.org/licenses/cpl1.0.php. 20 */ 21 22 #include "tpm_utils.h" 23 24 int iLogLevel = LOG_LEVEL_ERROR; 25 26 int logHex(int a_iLen, void *a_pData) 27 { 28 29 int i, iByte; 30 char *pData = a_pData; 31 32 for (i = 0; i < a_iLen; i++) { 33 if ((i % 32) == 0) { 34 if (a_iLen > 32) { 35 logMsg("\n\t"); 36 } 37 } else if ((i % 4) == 0) { 38 logMsg(" "); 39 } 40 41 iByte = pData[i]; 42 iByte &= 0x000000ff; 43 logMsg("%02x", iByte); 44 } 45 46 logMsg("\n"); 47 48 return a_iLen; 49 } 50 51 int logMsg(const char *a_szFormat, ...) 52 { 53 54 int iCount; 55 va_list vaArgs; 56 57 va_start(vaArgs, a_szFormat); 58 iCount = logIt(stdout, a_szFormat, vaArgs); 59 va_end(vaArgs); 60 61 return iCount; 62 } 63 64 int logDebug(const char *a_szFormat, ...) 65 { 66 67 int iCount; 68 va_list vaArgs; 69 70 if (iLogLevel < LOG_LEVEL_DEBUG) 71 return 0; 72 73 va_start(vaArgs, a_szFormat); 74 iCount = logProcess(stdout, a_szFormat, vaArgs); 75 va_end(vaArgs); 76 77 return iCount; 78 } 79 80 int logInfo(const char *a_szFormat, ...) 81 { 82 83 int iCount; 84 va_list vaArgs; 85 86 if (iLogLevel < LOG_LEVEL_INFO) 87 return 0; 88 89 va_start(vaArgs, a_szFormat); 90 iCount = logProcess(stdout, a_szFormat, vaArgs); 91 va_end(vaArgs); 92 93 return iCount; 94 } 95 96 int logError(const char *a_szFormat, ...) 97 { 98 99 int iCount; 100 va_list vaArgs; 101 102 if (iLogLevel < LOG_LEVEL_ERROR) 103 return 0; 104 105 va_start(vaArgs, a_szFormat); 106 iCount = logProcess(stderr, a_szFormat, vaArgs); 107 va_end(vaArgs); 108 109 return iCount; 110 } 111 112 int logProcess(FILE * a_sStream, const char *a_szFormat, va_list a_vaArgs) 113 { 114 115 return logIt(a_sStream, a_szFormat, a_vaArgs); 116 } 117 118 int logIt(FILE * a_sStream, const char *a_szFormat, va_list a_vaArgs) 119 { 120 121 return vfprintf(a_sStream, a_szFormat, a_vaArgs); 122 } 123 124 void logSuccess(const char *a_cmd) 125 { 126 logInfo(_("%s succeeded\n"), a_cmd); 127 } 128 129 void logCmdOption(const char *aOption, const char *aDescr) 130 { 131 logMsg("\t%s\n\t\t%s\n", aOption, aDescr); 132 } 133 134 void logGenericOptions() 135 { 136 char *lOpt = NULL; 137 138 lOpt = malloc(16+strlen(LOG_NONE)+strlen(LOG_ERROR)+ 139 strlen(LOG_INFO)+strlen(LOG_DEBUG)); 140 if ( lOpt ) 141 sprintf( lOpt, "-l, --log [%s|%s|%s|%s]", LOG_NONE, LOG_ERROR, LOG_INFO, LOG_DEBUG ); 142 143 logCmdOption("-h, --help", _("Display command usage info.")); 144 logCmdOption("-v, --version", _("Display command version info.")); 145 logCmdOption( lOpt, _("Set logging level.")); 146 free ( lOpt ); 147 } 148 149 void logUnicodeCmdOption() 150 { 151 logCmdOption("-u, --unicode", _("Use TSS UNICODE encoding for passwords to comply with applications using TSS popup boxes")); 152 } 153 154 void logOwnerPassCmdOption() 155 { 156 logCmdOption("-o, --pwdo", _("Owner password")); 157 } 158 159 void logNVIndexCmdOption() 160 { 161 logCmdOption("-i, --index", _("Index of the NVRAM area")); 162 } 163 164 void logCmdHelp(const char *aCmd) 165 { 166 logMsg(_("Usage: %s [options]\n"), aCmd); 167 logGenericOptions(); 168 } 169 170 void logCmdHelpEx(const char *aCmd, char *aArgs[], char *aArgDescs[]) 171 { 172 int i; 173 174 logMsg(_("Usage: %s [options]"), aCmd); 175 for (i = 0; aArgs[i]; i++) 176 logMsg(" %s", aArgs[i]); 177 logMsg("\n"); 178 for (i = 0; aArgDescs[i]; i++) 179 logMsg("%s\n", aArgDescs[i]); 180 logGenericOptions(); 181 } 182 183 char *logBool(BOOL aValue) 184 { 185 return aValue ? _("true") : _("false"); 186 } 187