test.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding:utf-8 -*-
  2. import os
  3. import sys
  4. import time
  5. from ctypes.wintypes import HWND, LPCSTR, UINT
  6. from ctypes import *
  7. import chardet
  8. # 加载dll;
  9. logdll = CDLL(r'E:\bin\LogModule\logmodule.DLL')
  10. '''
  11. Python 2.x中,默认编码为ascii;
  12. 如果在文件中指定编码为utf-8,要转在gbk是无法直接转的;需要先转换成unicode,再转成gbk;
  13. Python 3.x,默认编码为unicode;
  14. 如果在文件中指定编码为uft-8,可以直接转gbk;
  15. '''
  16. if __name__ == '__main__':
  17. # time.sleep(20)
  18. # 启动服务;
  19. if logdll.StartServer(5566):
  20. print "启动服务成功"
  21. else:
  22. print "启动服务失败"
  23. # 启用写日志;
  24. logdll.EnableWriteLog(True)
  25. '''
  26. # 设置日志路径一;
  27. #
  28. # 错误方式:logdll.SetCaselogPath(r"E:\bin\城aa\我们aa.txt") C++默认编码为GBK, Python为utf-8,无法直接传递;
  29. #
  30. '''
  31. '''
  32. # 设置日志路径二;
  33. #
  34. # 正确方式:若python字符编码为unicode,直接将unicode转码为gbk
  35. #
  36. '''
  37. if 0:
  38. path = u"E:\\bin\\测试日志\\测试日志.txt"
  39. print type(path)
  40. logdll.SetCaselogPath(path.encode("gbk"))
  41. '''
  42. # 获取日志路径;
  43. '''
  44. logdll.GetCaselogPath.restype = POINTER(c_byte)
  45. path = logdll.GetCaselogPath()
  46. dir = string_at(path, -1).decode("gbk")
  47. print dir
  48. '''
  49. # 设置日志路径三;
  50. #
  51. # 正确方式:若python字符编码为utf-8,先将python字符utf-8转码为unicode,再将unicode转码为gbk
  52. #
  53. '''
  54. if 0:
  55. # 先解码utf-8为unicod,再转码为gbk;
  56. path = "E:\\bin\\城aa\\我们aa.txt"
  57. print type(path)
  58. print chardet.detect(path)
  59. logdll.SetCaselogPath(path.decode("utf-8").encode('gbk'))
  60. '''
  61. C接口: const int& len参数传递过程中会报错;改为int len不会报错;
  62. '''
  63. if 0:
  64. path = "E:\\bin\\城aa\\我们aa.txt"
  65. print chardet.detect(path)
  66. logdll.SetCaselogPath2(path.decode("utf-8").encode('gbk'), path.__len__())
  67. '''
  68. # 获取日志路径;
  69. '''
  70. logdll.GetCaselogPath.restype = POINTER(c_byte)
  71. path = logdll.GetCaselogPath()
  72. dir = string_at(path, -1).decode("utf-8")
  73. print dir
  74. index = 0
  75. # 获取最后的接收时间戳;
  76. logdll.GetReceivePrintTime.restype = c_int64
  77. while True:
  78. logdll.EnableWriteLog(True)
  79. time.sleep(0.1)
  80. path = r"E:\bin\测试日志\测试日志%d.txt" % index
  81. print "日志路径", path
  82. logdll.SetCaselogPath(path.decode("utf-8").encode('gbk'))
  83. dt = logdll.GetReceivePrintTime()
  84. print "时间:", dt, time.time(), time.time() - dt
  85. time.sleep(60)
  86. logdll.EnableWriteLog(False)
  87. time.sleep(0.1)
  88. index = index+1
  89. # 停止服务;
  90. logdll.StopServer()