diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-24 21:57:57 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-24 21:57:57 +0400 |
commit | e94425b9c4764ec545284e06f1046e6601ec9ea7 (patch) | |
tree | f1dd866e3c9f44d67503c2e5affb917fb946dc70 /src | |
parent | a7c82906934d7e640cda5c26448ce4fa232d2b46 (diff) |
+ Add message and block pprint for logging.
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Block.hs | 7 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Message.hs | 11 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Block.hs b/src/Network/BitTorrent/PeerWire/Block.hs index ddbc1020..9685a4fb 100644 --- a/src/Network/BitTorrent/PeerWire/Block.hs +++ b/src/Network/BitTorrent/PeerWire/Block.hs | |||
@@ -4,6 +4,7 @@ module Network.BitTorrent.PeerWire.Block | |||
4 | , defaultBlockSize | 4 | , defaultBlockSize |
5 | , pieceIx, blockIx | 5 | , pieceIx, blockIx |
6 | , blockRange, ixRange, isPiece | 6 | , blockRange, ixRange, isPiece |
7 | , ppBlockIx, ppBlock | ||
7 | 8 | ||
8 | , putInt, getInt | 9 | , putInt, getInt |
9 | ) where | 10 | ) where |
@@ -48,6 +49,10 @@ instance Serialize BlockIx where | |||
48 | putInt (ixLength ix) | 49 | putInt (ixLength ix) |
49 | {-# INLINE put #-} | 50 | {-# INLINE put #-} |
50 | 51 | ||
52 | ppBlockIx :: BlockIx -> String | ||
53 | ppBlockIx ix = "piece = " ++ show (ixPiece ix) ++ ", " | ||
54 | ++ "offset = " ++ show (ixOffset ix) ++ ", " | ||
55 | ++ "length = " ++ show (ixLength ix) | ||
51 | 56 | ||
52 | data Block = Block { | 57 | data Block = Block { |
53 | -- ^ Zero-based piece index. | 58 | -- ^ Zero-based piece index. |
@@ -60,6 +65,8 @@ data Block = Block { | |||
60 | , blkData :: ByteString | 65 | , blkData :: ByteString |
61 | } deriving (Show, Eq) | 66 | } deriving (Show, Eq) |
62 | 67 | ||
68 | ppBlock :: Block -> String | ||
69 | ppBlock = ppBlockIx . blockIx | ||
63 | 70 | ||
64 | -- | Widely used semi-official block size. | 71 | -- | Widely used semi-official block size. |
65 | defaultBlockSize :: Int | 72 | defaultBlockSize :: Int |
diff --git a/src/Network/BitTorrent/PeerWire/Message.hs b/src/Network/BitTorrent/PeerWire/Message.hs index 650e0082..c98c9808 100644 --- a/src/Network/BitTorrent/PeerWire/Message.hs +++ b/src/Network/BitTorrent/PeerWire/Message.hs | |||
@@ -1,5 +1,6 @@ | |||
1 | module Network.BitTorrent.PeerWire.Message | 1 | module Network.BitTorrent.PeerWire.Message |
2 | ( Message(..) | 2 | ( Message(..) |
3 | , ppMessage | ||
3 | ) where | 4 | ) where |
4 | 5 | ||
5 | import Control.Applicative | 6 | import Control.Applicative |
@@ -129,3 +130,13 @@ instance Serialize Message where | |||
129 | put (SuggestPiece pix) = putInt 5 >> putWord8 0x0D >> putInt pix | 130 | put (SuggestPiece pix) = putInt 5 >> putWord8 0x0D >> putInt pix |
130 | put (RejectRequest ix) = putInt 13 >> putWord8 0x10 >> put ix | 131 | put (RejectRequest ix) = putInt 13 >> putWord8 0x10 >> put ix |
131 | put (AllowedFast ix) = putInt 5 >> putWord8 0x11 >> putInt ix | 132 | put (AllowedFast ix) = putInt 5 >> putWord8 0x11 >> putInt ix |
133 | |||
134 | |||
135 | -- | Compact output for logging: only useful information but not payload bytes. | ||
136 | ppMessage :: Message -> String | ||
137 | ppMessage (Bitfield _) = "Bitfield " | ||
138 | ppMessage (Piece blk) = "Piece " ++ ppBlock blk | ||
139 | ppMessage (Cancel ix) = "Cancel " ++ ppBlockIx ix | ||
140 | ppMessage (SuggestPiece pix) = "Suggest" ++ show pix | ||
141 | ppMessage (RejectRequest ix) = "Reject" ++ ppBlockIx ix | ||
142 | ppMessage msg = show msg \ No newline at end of file | ||