Gintarus

loggi

Jul 6th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 KB | None | 0 0
  1. /* Decompiler 153ms, total 348ms, lines 333 */
  2. package io.netty.handler.logging;
  3.  
  4. import io.netty.buffer.ByteBuf;
  5. import io.netty.buffer.ByteBufHolder;
  6. import io.netty.channel.ChannelDuplexHandler;
  7. import io.netty.channel.ChannelHandlerContext;
  8. import io.netty.channel.ChannelPromise;
  9. import io.netty.channel.ChannelHandler.Sharable;
  10. import io.netty.util.internal.logging.InternalLogLevel;
  11. import io.netty.util.internal.logging.InternalLogger;
  12. import io.netty.util.internal.logging.InternalLoggerFactory;
  13. import java.net.SocketAddress;
  14.  
  15. @Sharable
  16. public class LoggingHandler extends ChannelDuplexHandler {
  17. private static final LogLevel DEFAULT_LEVEL;
  18. private static final String NEWLINE;
  19. private static final String[] BYTE2HEX;
  20. private static final String[] HEXPADDING;
  21. private static final String[] BYTEPADDING;
  22. private static final char[] BYTE2CHAR;
  23. protected final InternalLogger logger;
  24. protected final InternalLogLevel internalLevel;
  25. private final LogLevel level;
  26.  
  27. public LoggingHandler() {
  28. this(DEFAULT_LEVEL);
  29. }
  30.  
  31. public LoggingHandler(LogLevel level) {
  32. if (level == null) {
  33. throw new NullPointerException("level");
  34. } else {
  35. this.logger = InternalLoggerFactory.getInstance(this.getClass());
  36. this.level = level;
  37. this.internalLevel = level.toInternalLevel();
  38. }
  39. }
  40.  
  41. public LoggingHandler(Class<?> clazz) {
  42. this(clazz, DEFAULT_LEVEL);
  43. }
  44.  
  45. public LoggingHandler(Class<?> clazz, LogLevel level) {
  46. if (clazz == null) {
  47. throw new NullPointerException("clazz");
  48. } else if (level == null) {
  49. throw new NullPointerException("level");
  50. } else {
  51. this.logger = InternalLoggerFactory.getInstance(clazz);
  52. this.level = level;
  53. this.internalLevel = level.toInternalLevel();
  54. }
  55. }
  56.  
  57. public LoggingHandler(String name) {
  58. this(name, DEFAULT_LEVEL);
  59. }
  60.  
  61. public LoggingHandler(String name, LogLevel level) {
  62. if (name == null) {
  63. throw new NullPointerException("name");
  64. } else if (level == null) {
  65. throw new NullPointerException("level");
  66. } else {
  67. this.logger = InternalLoggerFactory.getInstance(name);
  68. this.level = level;
  69. this.internalLevel = level.toInternalLevel();
  70. }
  71. }
  72.  
  73. public LogLevel level() {
  74. return this.level;
  75. }
  76.  
  77. protected String format(ChannelHandlerContext ctx, String message) {
  78. String chStr = ctx.channel().toString();
  79. StringBuilder buf = new StringBuilder(chStr.length() + message.length() + 1);
  80. buf.append(chStr);
  81. buf.append(' ');
  82. buf.append(message);
  83. return buf.toString();
  84. }
  85.  
  86. public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
  87. if (this.logger.isEnabled(this.internalLevel)) {
  88. this.logger.log(this.internalLevel, this.format(ctx, "REGISTERED"));
  89. }
  90.  
  91. super.channelRegistered(ctx);
  92. }
  93.  
  94. public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
  95. if (this.logger.isEnabled(this.internalLevel)) {
  96. this.logger.log(this.internalLevel, this.format(ctx, "UNREGISTERED"));
  97. }
  98.  
  99. super.channelUnregistered(ctx);
  100. }
  101.  
  102. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  103. if (this.logger.isEnabled(this.internalLevel)) {
  104. this.logger.log(this.internalLevel, this.format(ctx, "ACTIVE"));
  105. }
  106.  
  107. super.channelActive(ctx);
  108. }
  109.  
  110. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  111. if (this.logger.isEnabled(this.internalLevel)) {
  112. this.logger.log(this.internalLevel, this.format(ctx, "INACTIVE"));
  113. }
  114.  
  115. super.channelInactive(ctx);
  116. }
  117.  
  118. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  119. if (this.logger.isEnabled(this.internalLevel)) {
  120. this.logger.log(this.internalLevel, this.format(ctx, "EXCEPTION: " + cause), cause);
  121. }
  122.  
  123. super.exceptionCaught(ctx, cause);
  124. }
  125.  
  126. public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  127. if (this.logger.isEnabled(this.internalLevel)) {
  128. this.logger.log(this.internalLevel, this.format(ctx, "USER_EVENT: " + evt));
  129. }
  130.  
  131. super.userEventTriggered(ctx, evt);
  132. }
  133.  
  134. public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
  135. if (this.logger.isEnabled(this.internalLevel)) {
  136. this.logger.log(this.internalLevel, this.format(ctx, "BIND(" + localAddress + ')'));
  137. }
  138.  
  139. super.bind(ctx, localAddress, promise);
  140. }
  141.  
  142. public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
  143. if (this.logger.isEnabled(this.internalLevel)) {
  144. this.logger.log(this.internalLevel, this.format(ctx, "CONNECT(" + remoteAddress + ", " + localAddress + ')'));
  145. }
  146.  
  147. super.connect(ctx, remoteAddress, localAddress, promise);
  148. }
  149.  
  150. public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
  151. if (this.logger.isEnabled(this.internalLevel)) {
  152. this.logger.log(this.internalLevel, this.format(ctx, "DISCONNECT()"));
  153. }
  154.  
  155. super.disconnect(ctx, promise);
  156. }
  157.  
  158. public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
  159. if (this.logger.isEnabled(this.internalLevel)) {
  160. this.logger.log(this.internalLevel, this.format(ctx, "CLOSE()"));
  161. }
  162.  
  163. super.close(ctx, promise);
  164. }
  165.  
  166. public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
  167. if (this.logger.isEnabled(this.internalLevel)) {
  168. this.logger.log(this.internalLevel, this.format(ctx, "DEREGISTER()"));
  169. }
  170.  
  171. super.deregister(ctx, promise);
  172. }
  173.  
  174. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  175. this.logMessage(ctx, "RECEIVED", msg);
  176. ctx.fireChannelRead(msg);
  177. }
  178.  
  179. public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
  180. this.logMessage(ctx, "WRITE", msg);
  181. ctx.write(msg, promise);
  182. }
  183.  
  184. public void flush(ChannelHandlerContext ctx) throws Exception {
  185. if (this.logger.isEnabled(this.internalLevel)) {
  186. this.logger.log(this.internalLevel, this.format(ctx, "FLUSH"));
  187. }
  188.  
  189. ctx.flush();
  190. }
  191.  
  192. private void logMessage(ChannelHandlerContext ctx, String eventName, Object msg) {
  193. if (this.logger.isEnabled(this.internalLevel)) {
  194. this.logger.log(this.internalLevel, this.format(ctx, this.formatMessage(eventName, msg)));
  195. }
  196.  
  197. }
  198.  
  199. protected String formatMessage(String eventName, Object msg) {
  200. if (msg instanceof ByteBuf) {
  201. return this.formatByteBuf(eventName, (ByteBuf)msg);
  202. } else {
  203. return msg instanceof ByteBufHolder ? this.formatByteBufHolder(eventName, (ByteBufHolder)msg) : this.formatNonByteBuf(eventName, msg);
  204. }
  205. }
  206.  
  207. protected String formatByteBuf(String eventName, ByteBuf buf) {
  208. int length = buf.readableBytes();
  209. int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
  210. StringBuilder dump = new StringBuilder(rows * 80 + eventName.length() + 16);
  211. dump.append(eventName).append('(').append(length).append('B').append(')');
  212. dump.append(NEWLINE + " +-------------------------------------------------+" + NEWLINE + " | 0 1 2 3 4 5 6 7 8 9 a b c d e f |" + NEWLINE + "+--------+-------------------------------------------------+----------------+");
  213. int startIndex = buf.readerIndex();
  214. int endIndex = buf.writerIndex();
  215.  
  216. int i;
  217. int remainder;
  218. int j;
  219. for(i = startIndex; i < endIndex; ++i) {
  220. remainder = i - startIndex;
  221. j = remainder & 15;
  222. if (j == 0) {
  223. dump.append(NEWLINE);
  224. dump.append(Long.toHexString((long)remainder & 4294967295L | 4294967296L));
  225. dump.setCharAt(dump.length() - 9, '|');
  226. dump.append('|');
  227. }
  228.  
  229. dump.append(BYTE2HEX[buf.getUnsignedByte(i)]);
  230. if (j == 15) {
  231. dump.append(" |");
  232.  
  233. for(int j = i - 15; j <= i; ++j) {
  234. dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
  235. }
  236.  
  237. dump.append('|');
  238. }
  239. }
  240.  
  241. if ((i - startIndex & 15) != 0) {
  242. remainder = length & 15;
  243. dump.append(HEXPADDING[remainder]);
  244. dump.append(" |");
  245.  
  246. for(j = i - remainder; j < i; ++j) {
  247. dump.append(BYTE2CHAR[buf.getUnsignedByte(j)]);
  248. }
  249.  
  250. dump.append(BYTEPADDING[remainder]);
  251. dump.append('|');
  252. }
  253.  
  254. dump.append(NEWLINE + "+--------+-------------------------------------------------+----------------+");
  255. return dump.toString();
  256. }
  257.  
  258. protected String formatNonByteBuf(String eventName, Object msg) {
  259. return eventName + ": " + msg;
  260. }
  261.  
  262. protected String formatByteBufHolder(String eventName, ByteBufHolder msg) {
  263. return this.formatByteBuf(eventName, msg.content());
  264. }
  265.  
  266. static {
  267. DEFAULT_LEVEL = LogLevel.DEBUG;
  268. NEWLINE = String.format("%n");
  269. BYTE2HEX = new String[256];
  270. HEXPADDING = new String[16];
  271. BYTEPADDING = new String[16];
  272. BYTE2CHAR = new char[256];
  273.  
  274. int i;
  275. StringBuilder buf;
  276. for(i = 0; i < 10; ++i) {
  277. buf = new StringBuilder(3);
  278. buf.append(" 0");
  279. buf.append(i);
  280. BYTE2HEX[i] = buf.toString();
  281. }
  282.  
  283. while(i < 16) {
  284. buf = new StringBuilder(3);
  285. buf.append(" 0");
  286. buf.append((char)(97 + i - 10));
  287. BYTE2HEX[i] = buf.toString();
  288. ++i;
  289. }
  290.  
  291. while(i < BYTE2HEX.length) {
  292. buf = new StringBuilder(3);
  293. buf.append(' ');
  294. buf.append(Integer.toHexString(i));
  295. BYTE2HEX[i] = buf.toString();
  296. ++i;
  297. }
  298.  
  299. StringBuilder buf;
  300. int j;
  301. int padding;
  302. for(i = 0; i < HEXPADDING.length; ++i) {
  303. padding = HEXPADDING.length - i;
  304. buf = new StringBuilder(padding * 3);
  305.  
  306. for(j = 0; j < padding; ++j) {
  307. buf.append(" ");
  308. }
  309.  
  310. HEXPADDING[i] = buf.toString();
  311. }
  312.  
  313. for(i = 0; i < BYTEPADDING.length; ++i) {
  314. padding = BYTEPADDING.length - i;
  315. buf = new StringBuilder(padding);
  316.  
  317. for(j = 0; j < padding; ++j) {
  318. buf.append(' ');
  319. }
  320.  
  321. BYTEPADDING[i] = buf.toString();
  322. }
  323.  
  324. for(i = 0; i < BYTE2CHAR.length; ++i) {
  325. if (i > 31 && i < 127) {
  326. BYTE2CHAR[i] = (char)i;
  327. } else {
  328. BYTE2CHAR[i] = '.';
  329. }
  330. }
  331.  
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment