diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java index 3528578de668d9a96001ed001177f440c489245a..55ce428f33a5b1cbe66c45b661b7d68698f0a651 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/adapter/LocalizationAdapter.java @@ -53,7 +53,7 @@ public class LocalizationAdapter { /** * Logger */ - private static final LogUtils logger = new LogUtils(LocalizationAdapter.class); + private static final LogUtils LOGGER = new LogUtils(LocalizationAdapter.class); /** * Params @@ -270,7 +270,7 @@ public class LocalizationAdapter { try { certificates = CertUtils.generateCertificates(FileUtils.readFile(certFile)); } catch (IOException | CertificateException | VerifyCertificateChainException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage() + "\nSolutions:" + "\n> The certificate format is incorrect, please check your appCertFile parameter."); diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java index bb4d1895d2339450e6da0d0afea229bcaa8feb7d..27fef50f041fbc09e73c078d88ecba833cb85cce 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/api/SignToolServiceImpl.java @@ -68,7 +68,7 @@ public class SignToolServiceImpl implements ServiceApi { /** * Logger. */ - private static final LogUtils logger = new LogUtils(ServiceApi.class); + private static final LogUtils LOGGER = new LogUtils(ServiceApi.class); static { Security.addProvider(new BouncyCastleProvider()); @@ -256,8 +256,8 @@ public class SignToolServiceImpl implements ServiceApi { FileUtils.write(p7b, new File(adapter.getOutFile())); isSuccess = true; } catch (IOException exception) { - logger.debug(exception.getMessage(), exception); - logger.error(exception.getMessage()); + LOGGER.debug(exception.getMessage(), exception); + LOGGER.error(exception.getMessage()); isSuccess = false; } return isSuccess; @@ -279,12 +279,12 @@ public class SignToolServiceImpl implements ServiceApi { VerificationResult verificationResult = verifyHelper.verify(p7b); isSign = verificationResult.isVerifiedPassed(); if (!isSign) { - logger.error(verificationResult.getMessage()); + LOGGER.error(verificationResult.getMessage()); } outputString(FileUtils.GSON_PRETTY_PRINT.toJson(verificationResult), adapter.getOutFile()); } catch (IOException exception) { - logger.debug(exception.getMessage(), exception); - logger.error(exception.getMessage()); + LOGGER.debug(exception.getMessage(), exception); + LOGGER.error(exception.getMessage()); isSign = false; } catch (VerifyException e) { CustomException.throwException(ERROR.VERIFY_ERROR, "Verify Profile Failed! " + e.getMessage()); @@ -309,7 +309,7 @@ public class SignToolServiceImpl implements ServiceApi { } else if ("remoteSign".equalsIgnoreCase(mode)) { signProvider = new RemoteSignProvider(); } else { - logger.info("Resign mode. But not implement yet"); + LOGGER.info("Resign mode. But not implement yet"); return false; } @@ -343,12 +343,12 @@ public class SignToolServiceImpl implements ServiceApi { */ public void outputString(String content, String file) { if (StringUtils.isEmpty(file)) { - logger.info(content); + LOGGER.info(content); } else { try { FileUtils.write(content.getBytes(StandardCharsets.UTF_8), new File(file)); } catch (IOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); } } @@ -364,14 +364,14 @@ public class SignToolServiceImpl implements ServiceApi { public boolean outputCert(X509Certificate certificate, String file) { try { if (StringUtils.isEmpty(file)) { - logger.info(CertUtils.generateCertificateInCer(certificate)); + LOGGER.info(CertUtils.generateCertificateInCer(certificate)); } else { FileUtils.write(CertUtils.generateCertificateInCer(certificate).getBytes(StandardCharsets.UTF_8), new File(file)); } return true; } catch (CertificateException | IOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } @@ -391,13 +391,13 @@ public class SignToolServiceImpl implements ServiceApi { stringBuilder.append(CertUtils.generateCertificateInCer(cert)); } if (StringUtils.isEmpty(file)) { - logger.info(stringBuilder.toString()); + LOGGER.info(stringBuilder.toString()); } else { FileUtils.write(stringBuilder.toString().getBytes(StandardCharsets.UTF_8), new File(file)); } return true; } catch (CertificateException | IOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java index 6ce87c4990035d8bb8f66a67d5c9f1d371b3e42b..97939ffd8c2e745e8df7c8c7d3fec9362b446781 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/cert/CertBuilder.java @@ -58,7 +58,7 @@ public class CertBuilder { /** * Logger. */ - private static final LogUtils logger = new LogUtils(ServiceApi.class); + private static final LogUtils LOGGER = new LogUtils(ServiceApi.class); /** * issuer keyPair. @@ -87,7 +87,7 @@ public class CertBuilder { try { request = new PKCS10CertificationRequest(csr); } catch (IOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.IO_CSR_ERROR, exception.getMessage()); } x509v3CertificateBuilder = new X509v3CertificateBuilder( @@ -99,10 +99,10 @@ public class CertBuilder { x509v3CertificateBuilder.addExtension(Extension.subjectKeyIdentifier, false, extUtils.createSubjectKeyIdentifier(request.getSubjectPublicKeyInfo())); } catch (NoSuchAlgorithmException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } catch (CertIOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } } @@ -122,10 +122,10 @@ public class CertBuilder { .getInstance(keyPair.getPublic().getEncoded()))); } } catch (NoSuchAlgorithmException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } catch (CertIOException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); } return this; @@ -216,16 +216,16 @@ public class CertBuilder { .getCertificate(x509v3CertificateBuilder.build(contentSigner)); cert.verify(keyPair.getPublic()); } catch (InvalidKeyException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.KEY_ERROR, exception.getMessage()); } catch (SignatureException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.SIGN_ERROR, exception.getMessage()); } catch (CertificateException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.CERTIFICATE_ERROR, exception.getMessage()); } catch (NoSuchAlgorithmException | NoSuchProviderException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.NOT_SUPPORT_ERROR, exception.getMessage()); } return cert; diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java index d6f5ed93da7be1a22f6c3f4b12a59bd7fde85deb..cccb93812e19b440fe205d7a7db0ce750c6af25d 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/signer/LocalSigner.java @@ -40,7 +40,7 @@ public class LocalSigner implements ISigner { /** * logger of LocalSigner */ - private static final LogUtils logger = new LogUtils(LocalSigner.class); + private static final LogUtils LOGGER = new LogUtils(LocalSigner.class); /** * PrivateKey. @@ -84,7 +84,7 @@ public class LocalSigner implements ISigner { signData = signature.sign(); } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException | SignatureException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.SIGN_ERROR, exception.getMessage()); } return signData; diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java index ed09baa8aef25daba8c00b8e773a88d2558e4459..16e6b3d3457d2af51236291eeec95e4c387b5b9c 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/KeyStoreHelper.java @@ -80,7 +80,7 @@ public class KeyStoreHelper { /** * Use LogManager to show log instead of use "System.out.print" to show log. */ - private static final LogUtils logger = new LogUtils(KeyStoreHelper.class); + private static final LogUtils LOGGER = new LogUtils(KeyStoreHelper.class); /** * Field keyStorePath. @@ -116,14 +116,14 @@ public class KeyStoreHelper { FileInputStream fis = null; try { if (FileUtils.isFileExist(keyStorePath)) { - logger.info("{} is exist. Try to load it with given passwd", keyStorePath); + LOGGER.info("{} is exist. Try to load it with given passwd", keyStorePath); fis = new FileInputStream(keyStorePath); keyStore.load(fis, pwd); } else { keyStore.load(null, null); } } catch (IOException | NoSuchAlgorithmException | CertificateException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.ACCESS_ERROR, "Init keystore failed: " + exception.getMessage() + "\nSolutions:" + "\n> The key store file does not exist, please check the key store file path." @@ -168,7 +168,7 @@ public class KeyStoreHelper { try { return keyStore.containsAlias(alias); } catch (KeyStoreException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); return false; } @@ -205,10 +205,10 @@ public class KeyStoreHelper { return (PrivateKey) key; } } catch (KeyStoreException | NoSuchAlgorithmException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.ACCESS_ERROR, exception.getMessage()); } catch (UnrecoverableKeyException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.ACCESS_ERROR, "Password error of '" + alias + "'"); } return null; @@ -228,7 +228,7 @@ public class KeyStoreHelper { try { cert.checkValidity(); } catch (CertificateExpiredException | CertificateNotYetValidException exception) { - logger.info("p12's certificates is not valid"); + LOGGER.info("p12's certificates is not valid"); } certificates.add(cert); } @@ -257,7 +257,7 @@ public class KeyStoreHelper { } } } catch (KeyStoreException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.KEYSTORE_OPERATION_ERROR, exception.getMessage()); } @@ -292,7 +292,7 @@ public class KeyStoreHelper { keyStore.store(fos, keyStorePwd); fos.flush(); } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.WRITE_FILE_ERROR, exception.getMessage()); return false; } @@ -319,7 +319,7 @@ public class KeyStoreHelper { return new JcaX509CertificateConverter().setProvider("BC") .getCertificate(certificateBuilder.build(contentSigner)); } catch (CertificateException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.IO_CERT_ERROR, exception.getMessage()); return null; } @@ -343,7 +343,7 @@ public class KeyStoreHelper { typeKeyStore = KeyStore.getInstance(KeyStore.getDefaultType()); } } catch (KeyStoreException exception) { - logger.debug(exception.getMessage(), exception); + LOGGER.debug(exception.getMessage(), exception); CustomException.throwException(ERROR.KEYSTORE_OPERATION_ERROR, exception.getMessage()); } return typeKeyStore; diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogFormatter.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogFormatter.java index 344a730daf5caf74d31d6cd0bed73fa134cc0606..ac0311363c87d275cc9b90595e45f86f47a92ccf 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogFormatter.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogFormatter.java @@ -15,7 +15,6 @@ package com.ohos.hapsigntool.utils; - import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -28,8 +27,7 @@ import java.util.logging.LogRecord; * @since 2024/12/08 */ public class LogFormatter extends Formatter { - private final static DateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); - + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); /** * format log. diff --git a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogUtils.java b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogUtils.java index 9a0610994f8af5b1abde81724925c190ac127377..825038ad8cb1dcaffad00bc9d15ed988efb71ef7 100644 --- a/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogUtils.java +++ b/hapsigntool/hap_sign_tool_lib/src/main/java/com/ohos/hapsigntool/utils/LogUtils.java @@ -27,9 +27,10 @@ import java.security.ProtectionDomain; import java.util.HashMap; import java.util.Map; import java.util.Properties; -import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.logging.StreamHandler; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,26 +40,37 @@ import java.util.regex.Pattern; * @since 2024/12/08 */ public class LogUtils { - private static Logger logger; private static Level level; private static final Map LEVEL_MAP = new HashMap<>(); private static final String DEFAULT_LEVEL = "info"; private static final Pattern PATTERN = Pattern.compile("\\{}"); + private static final Handler OUT_HANDLER; + private static final Handler ERR_HANDLER; + static { LEVEL_MAP.put(DEFAULT_LEVEL, Level.INFO); LEVEL_MAP.put("debug", Level.CONFIG); LEVEL_MAP.put("warn", Level.WARNING); LEVEL_MAP.put("error", Level.SEVERE); - String configFileName = "log.config"; + String configFileName = "log.properties"; try { level = LEVEL_MAP.get(getJarConfig(configFileName)); } catch (LogConfigException e) { level = LEVEL_MAP.get(getDefaultLogLevel(configFileName)); } + + LogFormatter logFormatter = new LogFormatter(); + OUT_HANDLER = new StreamHandler(System.out, logFormatter); + OUT_HANDLER.setFilter(record -> record.getLevel() != Level.SEVERE && record.getLevel() != Level.WARNING); + OUT_HANDLER.setLevel(level); + ERR_HANDLER = new StreamHandler(System.err, logFormatter); + ERR_HANDLER.setLevel(Level.WARNING); } + private final Logger logger; + /** * format log utils constructor. * @@ -67,10 +79,8 @@ public class LogUtils { public LogUtils(Class clazz) { logger = Logger.getLogger(clazz.getName()); logger.setUseParentHandlers(false); - ConsoleHandler consoleHandler = new ConsoleHandler(); - LogFormatter logFormatter = new LogFormatter(); - consoleHandler.setFormatter(logFormatter); - logger.addHandler(consoleHandler); + logger.addHandler(OUT_HANDLER); + logger.addHandler(ERR_HANDLER); logger.setLevel(level); } @@ -81,6 +91,7 @@ public class LogUtils { */ public void info(String log) { logger.info(" INFO - " + log); + OUT_HANDLER.flush(); } /** @@ -91,6 +102,7 @@ public class LogUtils { */ public void info(String log, Object arg) { logger.info(" INFO - " + replaceArgs(log, arg)); + OUT_HANDLER.flush(); } /** @@ -102,6 +114,7 @@ public class LogUtils { */ public void info(String log, Object arg1, Object arg2) { logger.info(" INFO - " + replaceArgs(log, arg1, arg2)); + OUT_HANDLER.flush(); } /** @@ -110,7 +123,8 @@ public class LogUtils { * @param log log string */ public void warn(String log) { - logger.warning(" WARN - " + log); + logger.warning(" WARN - " + log); + ERR_HANDLER.flush(); } /** @@ -120,7 +134,8 @@ public class LogUtils { * @param arg arg */ public void warn(String log, Object arg) { - logger.warning(" WARN - " + replaceArgs(log, arg)); + logger.warning(" WARN - " + replaceArgs(log, arg)); + ERR_HANDLER.flush(); } /** @@ -132,6 +147,7 @@ public class LogUtils { */ public void warn(String log, Object arg, Throwable e) { logger.log(Level.WARNING, e, () -> " DEBUG - " + replaceArgs(log, arg)); + ERR_HANDLER.flush(); } /** @@ -142,7 +158,8 @@ public class LogUtils { * @param arg2 arg */ public void warn(String log, Object arg1, Object arg2) { - logger.warning(" WARN - " + replaceArgs(log, arg1, arg2)); + logger.warning(" WARN - " + replaceArgs(log, arg1, arg2)); + ERR_HANDLER.flush(); } /** @@ -153,6 +170,7 @@ public class LogUtils { */ public void warn(String log, Throwable e) { logger.log(Level.WARNING, e, () -> " DEBUG - " + log); + ERR_HANDLER.flush(); } /** @@ -161,7 +179,8 @@ public class LogUtils { * @param log log string */ public void debug(String log) { - logger.config(" DEBUG - " + log); + logger.config(" DEBUG - " + log); + OUT_HANDLER.flush(); } /** @@ -172,7 +191,8 @@ public class LogUtils { * @param arg2 arg */ public void debug(String log, Object arg1, Object arg2) { - logger.warning(" WARN - " + replaceArgs(log, arg1, arg2)); + logger.warning(" WARN - " + replaceArgs(log, arg1, arg2)); + OUT_HANDLER.flush(); } /** @@ -183,6 +203,7 @@ public class LogUtils { */ public void debug(String log, Throwable e) { logger.log(Level.CONFIG, e, () -> " DEBUG - " + log); + OUT_HANDLER.flush(); } /** @@ -192,7 +213,8 @@ public class LogUtils { * @param arg arg */ public void debug(String log, Object arg) { - logger.config(" DEBUG - " + replaceArgs(log, arg)); + logger.config(" DEBUG - " + replaceArgs(log, arg)); + OUT_HANDLER.flush(); } /** @@ -201,7 +223,8 @@ public class LogUtils { * @param log log string */ public void error(String log) { - logger.severe(" ERROR - " + log); + logger.severe(" ERROR - " + log); + ERR_HANDLER.flush(); } /** @@ -212,6 +235,7 @@ public class LogUtils { */ public void error(String log, Throwable e) { logger.log(Level.SEVERE, e, () -> " ERROR - " + log); + ERR_HANDLER.flush(); } /** @@ -223,6 +247,7 @@ public class LogUtils { */ public void error(String log, Object arg, Throwable e) { logger.log(Level.SEVERE, e, () -> " ERROR - " + replaceArgs(log, arg)); + ERR_HANDLER.flush(); } /** @@ -232,7 +257,8 @@ public class LogUtils { * @param arg arg */ public void error(String log, Object arg) { - logger.severe(" ERROR - " + replaceArgs(log, arg)); + logger.severe(" ERROR - " + replaceArgs(log, arg)); + ERR_HANDLER.flush(); } /** @@ -243,7 +269,8 @@ public class LogUtils { * @param arg2 arg */ public void error(String log, Object arg1, Object arg2) { - logger.severe(" ERROR - " + replaceArgs(log, arg1, arg2)); + logger.severe(" ERROR - " + replaceArgs(log, arg1, arg2)); + ERR_HANDLER.flush(); } /** @@ -255,7 +282,8 @@ public class LogUtils { * @param arg3 arg */ public void error(String log, Object arg1, Object arg2, Object arg3) { - logger.severe(" ERROR - " + replaceArgs(log, arg1, arg2, arg3)); + logger.severe(" ERROR - " + replaceArgs(log, arg1, arg2, arg3)); + ERR_HANDLER.flush(); } private static String replaceArgs(String line, Object... args) { diff --git a/tools/auto_test.py b/tools/auto_test.py index d20f8175b850f6d4e98b6940cf3f978578648b6e..693b2ff52cf3b4b5cebcd16f9929d1cb675494ce 100644 --- a/tools/auto_test.py +++ b/tools/auto_test.py @@ -21,8 +21,8 @@ import sys import time import ast import re +import tempfile from subprocess import Popen -from subprocess import PIPE def print_help(): @@ -274,29 +274,29 @@ def run_target(case, cmd): case_result['times'] = case_result['times'] + 1 start = time.time() - command = Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=False) + out_temp = tempfile.TemporaryFile(mode='w+') + command = Popen(cmd, stdout=out_temp, stderr=out_temp, stdin=out_temp, shell=False) + success = True + code = command.wait() + if code != 0: + success = False - out = command.stdout.readlines() - with open("log.txt", mode='a+', encoding='utf-8') as f: + log_name = "log.txt" + if not success: + log_name = "error.txt" + + out_temp.seek(0) + out = out_temp.readlines() + with open(log_name, mode='a+', encoding='utf-8') as f: if len(out) > 0: f.writelines(' '.join(cmd) + "\r\n") for line in out: f.writelines(str(line.strip()) + "\r\n") + out_temp.close() - success = True - error = command.stderr.readlines() - with open("error.txt", mode='a+', encoding='utf-8') as f: - if len(error) > 0: - f.writelines(' '.join(cmd) + "\r\n") - for line in error: - f.writelines(str(line.strip()) + "\r\n") - - code = command.wait() - if code != 0: - success = False end = time.time() case_result['total_cost'] = case_result['total_cost'] + (end - start) - + out_temp.close() if success: case_result['success'] = case_result['success'] + 1 else: